Armstrong Number

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

  1. // To check armstrong number
  2. import java.io.*;
  3. import java.lang.Math.*;
  4. public class armstrong
  5. {
  6. public static void main(String args[]) throws Exception
  7. {
  8. int num, p, temp = 0, sum = 0, r =0;
  9. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10. System.out.println("Enter a new number :");
  11. num = Integer.parseInt(br.readLine());
  12. temp = num;
  13. p = (int)(Math.log10(num)+1); //gets number of digits
  14. while(num>0) // going to process only > 0
  15. {
  16. r = num%10; // returns first place number
  17. sum = sum + (int)(java.lang.Math.pow(r,p));
  18. //takes power of p digits to the number
  19. num = num/10; //removes the first place number
  20. }
  21. if (temp == sum)
  22. System.out.println("It is Armstrong Number : " +temp);
  23. else
  24. System.out.println("It is Not Armstrong Number :" +temp);
  25. }
  26. }