Java Program to Identify Armstrong Numbers Using For Loop
What Is an Armstrong Number?
An Armstrong number (also known as a narcissistic number) is a number that equals the sum of its own digits each raised to the power of the total number of digits.
Mathematically, for a number with digits x, y, … z and n digits, the following holds:
xy…z = xn + yn + … + zn
Examples of Armstrong numbers include:
0, 1, 4, 5, 9, 153, 371, 407, 8208, …
Java Program to Check If a Number Is an Armstrong Number
Below is a compact Java implementation that verifies whether a given integer is an Armstrong number. The algorithm iterates over each digit, accumulates the cube of the digit (since we’re focusing on three‑digit numbers), and compares the sum to the original number.
// Check whether a number is an Armstrong number using a while loop
package com.guru99;
public class ArmstrongNumber {
public static void main(String[] args) {
int inputArmstrongNumber = 153; // Number to test
int tempNumber, digit, digitCubeSum = 0;
tempNumber = inputArmstrongNumber;
while (tempNumber != 0) {
System.out.println("Current Number is " + tempNumber);
digit = tempNumber % 10;
System.out.println("Current Digit is " + digit);
digitCubeSum += digit * digit * digit;
System.out.println("Current digitCubeSum is " + digitCubeSum);
tempNumber /= 10;
}
if (digitCubeSum == inputArmstrongNumber)
System.out.println(inputArmstrongNumber + " is an Armstrong Number");
else
System.out.println(inputArmstrongNumber + " is not an Armstrong Number");
}
}
Output
Current Number is 153 Current Digit is 3 Current digitCubeSum is 27 Current Number is 15 Current Digit is 5 Current digitCubeSum is 152 Current Number is 1 Current Digit is 1 Current digitCubeSum is 153 153 is an Armstrong Number
Java Program to List All Armstrong Numbers From 0 to 999
The following program demonstrates how to enumerate every Armstrong number in the 0‑999 range. It leverages a for loop for the range and a while loop to process each candidate.
// Enumerate Armstrong numbers between 0 and 999
package com.guru99;
public class ArmstrongNumber {
public static void main(String[] args) {
for (int inputArmstrongNumber = 0; inputArmstrongNumber < 1000; inputArmstrongNumber++) {
int tempNumber = inputArmstrongNumber;
int digitCubeSum = 0;
while (tempNumber != 0) {
int digit = tempNumber % 10;
digitCubeSum += digit * digit * digit;
tempNumber /= 10;
}
if (digitCubeSum == inputArmstrongNumber)
System.out.println(inputArmstrongNumber + " is an Armstrong Number");
}
}
}
Output
0 is an Armstrong Number 1 is an Armstrong Number 153 is an Armstrong Number 370 is an Armstrong Number 371 is an Armstrong Number 407 is an Armstrong Number
Java
- Java Hello World: Your First Program
- Master Java For Loops: Syntax, Examples, and Best Practices
- Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
- Java Continue Statement – Mastering Loop Control with Examples
- Java For‑Each Loop: Simplifying Array Iteration Without Counters
- Java Program to Determine If a Number Is Prime
- Java Program to List Prime Numbers from 1 to 100 – Step‑by‑Step Guide
- Generate Fibonacci Sequence in Java: For, While, and Recursive Examples
- Java Palindrome Number Checker: Algorithms Using While and For Loops
- Mastering Java Loop Control: Efficient Repetition Techniques