Armstrong Number in Java

Armstrong Number:

Armstrong Number is three digits of a positive number where the sum of the cubes of its digits is equal to the number itself.

import java.util.Scanner;
public class Armstrong_Number
{
public static void main(String[] args)
{
int n, Sum = 0, num, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
Sum = Sum + (r * r * r);
num = num / 10;
}
if (n == Sum)
{
System.out.println("It's An Armstrong Number!");
}
else
{
System.out.println("It's Not Armstrong Number!");
}
}
}

 

Output:

Armstrong Number in Java

Leave a Reply

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