0
0
JavaProgramBeginner · 2 min read

Java Program to Check Armstrong Number

A Java program to check an Armstrong number reads a number, calculates the sum of cubes of its digits using while loop and Math.pow, then compares the sum with the original number to decide if it is Armstrong.
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is Armstrong, break it into digits, raise each digit to the power of the number of digits, sum these values, and compare the sum to the original number. If they match, the number is Armstrong.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
Initialize sum to zero.
4
Extract each digit, raise it to the power of digit count, and add to sum.
5
Compare sum with original number.
6
Print if the number is Armstrong or not.
💻

Code

java
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();
    }
}
Output
153 153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code

1

Input and Initialization

num = 153, original = 153, sum = 0, digits = 3

2

First digit extraction

digit = 153 % 10 = 3; sum = 0 + 3^3 = 27; num = 153 / 10 = 15

3

Second digit extraction

digit = 15 % 10 = 5; sum = 27 + 5^3 = 27 + 125 = 152; num = 15 / 10 = 1

4

Third digit extraction

digit = 1 % 10 = 1; sum = 152 + 1^3 = 152 + 1 = 153; num = 1 / 10 = 0

5

Comparison

sum = 153 equals original = 153, so number is Armstrong

DigitSum after adding digit^digits
327
5152
1153
💡

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

Using String conversion and for-loop
java
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();
    }
}
This approach uses string manipulation instead of arithmetic, which can be simpler but less efficient.
Using recursion to calculate sum
java
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();
    }
}
This recursive method is elegant but may be less efficient and harder to understand for beginners.

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.

ApproachTimeSpaceBest For
Arithmetic loopO(d)O(1)Simple and efficient
String conversionO(d)O(d)Easier to read but uses extra space
RecursionO(d)O(d)Elegant but uses call stack
💡
Use Math.pow(digit, digits) to raise each digit to the correct power easily.
⚠️
Beginners often forget to count the number of digits first and raise digits to that power.