Armstrong Number in Java - Java Program for to Check Armstrong Number
Armstrong Number in Java :
- An Armstrong number is a number that is equal to the sum of the cubes of its own digits. Armstrong number of order n if it's every digit separate out and cubed and summed up then the sum will be same as the number .i.e. 153 = 13 + 53 + 33 = 1+125+27 = 153
Sample Code
import java.util.Scanner;
public class armstrong
{
public static void main(String[] args)
{
int n, r, sum = 0, temp;
System.out.println("Enter the Number= ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
System.out.println("Armstrong Number.");
else
System.out.println("Not Armstrong Number.");
}
}
Output
Enter the Number= 371
Armstrong Number.
Enter the Number= 333
Not Armstrong Number.