Theory
Let a1a2a3…an a number of n digits, where a1, a2, a3,…an are the digits of the number. The number will be called if
a1n+a2n+a3n+a4n+…….+ann = a1a2a3…an (the number itself)
There is only 1 digit in the number from 0 to 9. Now notice that
01 = 0; 11 = 1; 21 = 2; 31 = 3; 41 = 4; 51 = 5; 61 = 6; 71 = 7; 81 = 8; 91 = 9;
So these are all Armstrong numbers. But
12+02 ≠ 10; 12+12 ≠ 11; 12+22 ≠ 12
So these three numbers and many more are not Armstrong. Here are a few more examples of Armstrong numbers:
13+53+33 = 153; 33+73+03 = 370; 33+73+13 = 371; 43+03+73 = 407
So 153, 370, 371 and 407 are also Armstrong numbers.
Java Code
package com.physicsinfo; import java.util.Scanner; public class Main { public static void main(String args[]){ System.out.print("Enter the number you want to check for being Armstrong: "); Scanner sc = new Scanner(System.in); String str = sc.next(); int n = str.length(); char[] chr = new char[n]; int[] num = new int[n]; for (int i=0;i<n; i++){ chr[i] = str.charAt(i); num[i] = Character.getNumericValue(chr[i]); } double sum = 0; for(int j=0;j<n;j++){ sum = sum +Math.pow(num[j],n); } if (sum == Double.parseDouble(str)){ System.out.println("It's a Armstrong number."); } else{ System.out.println("It's not a Armstrong number."); } } }