Armstrong Number Program in Java
In this program, you will understand and get the code of simple Armstrong number program in java.
An Armstrong number is a number that is equal to the sum of its own digits when each digit is raised to the power of the number of digits in the number.
For example:
153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Example of Armstrong Number Program in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //Armstrong Number Program in Java import java.util.*; public class ArmstrongNumber { public static void main(String[] args) { int num; int realNumber, rem, res = 0; Scanner in = new Scanner(System.in); System.out.println("Enter a number : "); num = in.nextInt(); realNumber = num; while (num != 0) { rem = num % 10; res += (Math.pow(rem , 3)); num = num / 10; } if(res == realNumber) System.out.println(realNumber + " is an Armstrong number"); else System.out.println(realNumber + " is not an Armstrong number"); } } |
Output
C:\CodeRevise\java>javac ArmstrongNumber.java
C:\CodeRevise\java>java ArmstrongNumber
Enter a number :
153
153 is an Armstrong number
C:\CodeRevise\java>java ArmstrongNumber
Enter a number :
125
125 is not an Armstrong number
Check out our other Java Programming Examples