Java Program to Check Armstrong Number
while loop and Math.pow, then compares the sum with the original number to decide if it is Armstrong.Examples
How to Think About It
Algorithm
Code
import java.util.Scanner; public class ArmstrongCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int original = num, sum = 0; int digits = String.valueOf(num).length(); while (num > 0) { int digit = num % 10; sum += Math.pow(digit, digits); num /= 10; } if (sum == original) { System.out.println(original + " is an Armstrong number"); } else { System.out.println(original + " is not an Armstrong number"); } sc.close(); } }
Dry Run
Let's trace the number 153 through the code
Input and Initialization
num = 153, original = 153, sum = 0, digits = 3
First digit extraction
digit = 153 % 10 = 3; sum = 0 + 3^3 = 27; num = 153 / 10 = 15
Second digit extraction
digit = 15 % 10 = 5; sum = 27 + 5^3 = 27 + 125 = 152; num = 15 / 10 = 1
Third digit extraction
digit = 1 % 10 = 1; sum = 152 + 1^3 = 152 + 1 = 153; num = 1 / 10 = 0
Comparison
sum = 153 equals original = 153, so number is Armstrong
| Digit | Sum after adding digit^digits |
|---|---|
| 3 | 27 |
| 5 | 152 |
| 1 | 153 |
Why This Works
Step 1: Counting digits
We find the number of digits to know the power to raise each digit.
Step 2: Summing powers
Each digit is raised to the power of digit count and added to sum to check Armstrong condition.
Step 3: Final check
If the sum equals the original number, it confirms the number is Armstrong.
Alternative Approaches
import java.util.Scanner; public class ArmstrongCheckAlt { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String numStr = sc.next(); int digits = numStr.length(); int sum = 0; for (int i = 0; i < digits; i++) { int digit = numStr.charAt(i) - '0'; sum += Math.pow(digit, digits); } int original = Integer.parseInt(numStr); if (sum == original) { System.out.println(original + " is an Armstrong number"); } else { System.out.println(original + " is not an Armstrong number"); } sc.close(); } }
import java.util.Scanner; public class ArmstrongCheckRec { public static int power(int base, int exp) { if (exp == 0) return 1; return base * power(base, exp - 1); } public static int sumOfPowers(int num, int digits) { if (num == 0) return 0; int digit = num % 10; return power(digit, digits) + sumOfPowers(num / 10, digits); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int digits = String.valueOf(num).length(); int sum = sumOfPowers(num, digits); if (sum == num) { System.out.println(num + " is an Armstrong number"); } else { System.out.println(num + " is not an Armstrong number"); } sc.close(); } }
Complexity: O(d) time, O(1) space
Time Complexity
The program loops through each digit once, so time depends linearly on the number of digits, O(d).
Space Complexity
Only a few variables are used, so space complexity is constant, O(1).
Which Approach is Fastest?
The arithmetic loop approach is fastest and simplest; string-based or recursive methods add overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Arithmetic loop | O(d) | O(1) | Simple and efficient |
| String conversion | O(d) | O(d) | Easier to read but uses extra space |
| Recursion | O(d) | O(d) | Elegant but uses call stack |
Math.pow(digit, digits) to raise each digit to the correct power easily.