Java Program to Check whether Given Number is Armstrong or Not
Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.
For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634
and So on……..
SOURCE CODE ::
import java.util.Scanner;
import java.lang.Math;
public class Armstrong {
public static void main(String[] args) {
int a,b,n,arm=0,temp;
System.out.print("Enter No. which u want to Check :");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
temp=n;
a=countdigit(n);
while(n>0)
{
b=n%10;
n=n/10;
arm = (int) (arm + Math.pow(b, a)) ;
}
if(arm==temp)
{
System.out.println("Armstrong No.");
}
else
{
System.out.println("Not Armstrong No.");
}
}
static int countdigit(int n)
{
int c=0;
while(n>0)
{
n=n/10;
c++;
}
return c;
}
}
OUTPUT ::
Enter No. which u want to Check :153 Armstrong No. ----------------------------------------------------------------------------------- Enter No. which u want to Check :222 Not Armstrong No. ----------------------------------------------------------------------------------- Enter No. which u want to Check :1741725 Armstrong No.