58. JAVA Program to find the given number is Armstrong number or Not.
How does this program work?
- In this program you are going to learn about to check if a number is Armstrong number or not using JAVA.
- An armstrong number is a number whose value is equal to the sum of the cubes of its digits.
- If sum is equal to the original number, then that number is armstrong number else the number is not a armstrong number.
Here is the code
// Armstrong number or not.
import java.util.Scanner;public class Armstrong
{
public static void main(String[] arg )
{
int a,arm=0,n,temp;
System.out.println("Enter a number:\n");
Scanner obj=new Scanner( System.in);
n=obj.nextInt();
temp=n;
for( ;n!=0;n/=10 )
{
a=n%10; arm=arm+(a*a*a);
}
if(arm==temp)
System.out.println(temp+" is a armstrong number ");
else
System.out.println(temp+" is not a armstrong number ");
}
}