Introduction
An Armstrong Number (also known as a Narcissistic Number) is a number that is equal to the sum of its own digits, where each digit is raised to the power of the total number of digits in the number.
For example:
153 is an Armstrong number because:
153 = 1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Another example:
9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴
= 6561 + 256 + 2401 + 256
= 9474
Examples of Armstrong numbers:
0, 1, 153, 370, 371, 407, 1634, 8208, 9474
Examples of non-Armstrong numbers:
100
123
250
500
The Armstrong Number program is one of the most frequently asked Java interview programs. It helps beginners understand loops, arithmetic operators, mathematical functions, and number manipulation.
In this tutorial, you’ll learn different ways to check whether a number is an Armstrong number in Java.
Java Program to Check Armstrong Number
public class ArmstrongNumber {
public static void main(String[] args) {
int number = 153;
int originalNumber = number;
int result = 0;
int digits = String.valueOf(number).length();
while (number != 0) {
int digit = number % 10;
result += Math.pow(digit, digits);
number /= 10;
}
if (result == originalNumber) {
System.out.println(originalNumber + " is an Armstrong Number");
} else {
System.out.println(originalNumber + " is not an Armstrong Number");
}
}
}
Output
How the Program Works?
Let’s understand the logic step by step.
Step 1: Store the Number
int number = 153;
This is the number to be checked.
Step 2: Save the Original Number
int originalNumber = number;
The original number is stored because the value of number changes during processing.
Step 3: Count the Number of Digits
int digits = String.valueOf(number).length();
For 153, the number of digits is 3.
Step 4: Extract Each Digit
int digit = number % 10;
For 153:
| Number | Digit |
|---|---|
| 153 | 3 |
| 15 | 5 |
| 1 | 1 |
Step 5: Raise the Digit to the Required Power
result += Math.pow(digit, digits);
Calculation:
3³ = 27
5³ = 125
1³ = 1
Sum:
27 + 125 + 1 = 153
Step 6: Compare the Result
if(result == originalNumber)
If both values are equal, the number is an Armstrong number.
Java Program Using User Input
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number;
int result = 0;
int digits = String.valueOf(number).length();
while (number != 0) {
int digit = number % 10;
result += Math.pow(digit, digits);
number /= 10;
}
if (result == originalNumber)
System.out.println(originalNumber + " is an Armstrong Number");
else
System.out.println(originalNumber + " is not an Armstrong Number");
scanner.close();
}
}
Output
9474 is an Armstrong Number
Java Program to Print Armstrong Numbers Between 1 and 1000
public class ArmstrongNumbers {
public static void main(String[] args) {
System.out.println("Armstrong Numbers from 1 to 1000:");
for (int number = 1; number <= 1000; number++) {
int original = number;
int temp = number;
int result = 0;
int digits = String.valueOf(number).length();
while (temp != 0) {
int digit = temp % 10;
result += Math.pow(digit, digits);
temp /= 10;
}
if (result == original)
System.out.print(original + " ");
}
}
}
Output
1 2 3 4 5 6 7 8 9 153 370 371 407
Algorithm
- Read the number.
- Store the original number.
- Count the total number of digits.
- Extract each digit using the modulus (%) operator.
- Raise each digit to the power of the total number of digits.
- Add the powered values.
- Compare the sum with the original number.
- Print whether the number is an Armstrong number.
Flowchart Logic
Start
│
Read Number
│
Count Digits
│
Extract Each Digit
│
Calculate digit^digits
│
Add All Values
│
Compare with Original Number
│
Equal?
│
Yes ──► Armstrong Number
│
No
│
Not Armstrong Number
│
End
Time Complexity
Checking one Armstrong number:
O(d)
where d is the number of digits.
For an n-digit number, the loop executes once for each digit.
Space Complexity
O(1)
Only a few variables are used.
Common Mistakes
1. Forgetting to Store the Original Number
Incorrect:
int number = 153;
After processing, number becomes 0.
Correct:
int originalNumber = number;
2. Hardcoding the Power
Incorrect:
result += Math.pow(digit, 3);
This works only for three-digit numbers.
Correct:
result += Math.pow(digit, digits);
3. Using Integer Division Incorrectly
Incorrect:
number = number % 10;
Correct:
number = number / 10;
4. Comparing the Wrong Variable
Incorrect:
if(result == number)
After the loop, number becomes 0.
Correct:
if(result == originalNumber)
5. Ignoring Single-Digit Numbers
Numbers from 0 to 9 are Armstrong numbers because:
5¹ = 5
8¹ = 8
Applications of Armstrong Numbers
Although Armstrong numbers are primarily mathematical curiosities, they are useful for:
- Learning number manipulation
- Understanding loops
- Practicing mathematical functions
- Coding interviews
- Competitive programming
- Educational programming exercises
Summary
The Java Armstrong Number program is an excellent exercise for understanding number manipulation, loops, mathematical calculations, and conditional statements. The program extracts each digit, raises it to the power of the total number of digits, sums the results, and compares the sum with the original number.
By mastering this program, you'll strengthen your understanding of Java fundamentals and be better prepared for coding interviews.