Computer Applications

Write a Java Program to define a function which accept a number and check it is a Armstrong number or not

Write a Java Program to define a function which accept a number and check it is a Armstrong number or not.

import java.io.*;
import java.util.*;

class Armstrong
{
	void check( int n )
	{
		int rem=0, rev=0, n1=n;
		while( n>0 )
		{
			rem = n % 10;
			rev = rev + rem * rem * rem ;
			n = n / 10;
		}
		if( n1 == rev)
		  System.out.println(" Number is Armstrong.");
		else
		  System.out.println(" Number is not Armstrong.");
	}
  public static void main(String args[ ])
  {
  	Scanner sc = new Scanner(System.in);
  	int n;
  	System.out.println("Enter n =");
  	n = sc.nextInt();
  	Armstrong ob = new Armstrong( );
  	ob.check( n );
  }
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top