First of all we need to understand what Armstrong Number is. Armstrong number is the number
which is the sum of the cubes of all its unit, tens and hundred digits for three digit number.
which is the sum of the cubes of all its unit, tens and hundred digits for three digit number.
153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
1 | class Armstrong{ |
2 | public static void main(String[] args) { |
3 | int c= 0 ,a,temp; |
4 | int n= 153 ; //It is the number to check Armstrong |
5 | temp=n; |
6 | while (n> 0 ) |
7 | { |
8 | a=n% 10 ; |
9 | n=n/ 10 ; |
10 | c=c+(a*a*a); |
11 | } |
12 | if (temp==c) |
13 | System.out.println( "armstrong number" ); |
14 | else |
15 | System.out.println( "Not armstrong number" ); |
16 | } |
17 | } |
Output:
armstrong number
armstrong number