Submitted by admin on 22 November, 2011 - 00:31
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
Example : 14 + 64 + 34 + 44 = 1634. 374
// To check armstrong number import java.io.*; import java.lang.Math.*; public class armstrong { public static void main(String args[]) throws Exception { int num, p, temp = 0, sum = 0, r =0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a new number :"); num = Integer.parseInt(br.readLine()); temp = num; p = (int)(Math.log10(num)+1); //gets number of digits while(num>0) // going to process only > 0 { r = num%10; // returns first place number sum = sum + (int)(java.lang.Math.pow(r,p)); //takes power of p digits to the number num = num/10; //removes the first place number } if (temp == sum) System.out.println("It is Armstrong Number : " +temp); else System.out.println("It is Not Armstrong Number :" +temp); } }