Java program to check if a number is Armstrong or not

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
Armstrong number:0,1,153,370,371,407
i.e
153=(1*1*1)+(5*5*5)+(3*3*3)

Java program to check if a number is Armstrong or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package logic;
 
import java.util.Scanner;
 
public class Armstrong 
{
 
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
 
 
         Scanner c=new Scanner(System.in);
 
	System.out.println("enter a number");
 
        int a=c.nextInt();
 
        int temp=a;
 
        int  sum=0;
 
 
        while(a!=0)
        {
 
        	int d=a%10;
        	sum=sum+d*d*d;
        	a=a/10;
 
        }
 
        if(sum==temp)
        {
        	System.out.println("it is armstrong number");
        }
 
        else
        {
        	System.out.println("not armstrong");
        }
	}
 
}

output:

1
2
3
enter a number
153
it is armstrong number

Leave a Reply

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