‹ All Computer Science notes
ISC Practicals and Theory - Armstrong Number
Updated 20 Oct 2026
An Armstrong number is a number that equals the sum of its own digits, each raised to the power of the number of digits. This program accepts a number, counts its digits, adds up the powers and compares the sum with the number, all inside main.
Definition
A number $N$ with $k$ digits is an Armstrong number if the sum of the $k$-th powers of its digits is equal to $N$.
- $153$ has 3 digits: $1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153$. So 153 is an Armstrong number.
- $125$ has 3 digits: $1^3 + 2^3 + 5^3 = 1 + 8 + 125 = 134$, which is not $125$. So 125 is not an Armstrong number.
Algorithm
- Accept the number
nusingScanner. - If
n <= 0, display an invalid input message and stop. - Count the digits of
n:- Copy
ntotempand setdigits = 0. - While
temp > 0, increasedigitsby 1 and dotemp = temp / 10.
- Copy
- Copy
ntotempagain and setsum = 0. - While
temp > 0:- Extract the last digit:
d = temp % 10. - Find $d^{digits}$ in
pby multiplyingpbyda total ofdigitstimes, using aforloop. - Add
ptosum. - Remove the last digit:
temp = temp / 10.
- Extract the last digit:
- If
sum == n, display that it is an Armstrong number, otherwise display that it is not.
Program
The whole logic is written in the main method of class Armstrong.
import java.util.Scanner;
public class Armstrong
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n <= 0)
{
System.out.println("Invalid input. Enter a positive number.");
}
else
{
int temp = n, digits = 0;
while (temp > 0)
{
digits++;
temp = temp / 10;
}
temp = n;
int sum = 0;
while (temp > 0)
{
int d = temp % 10;
int p = 1;
for (int i = 1; i <= digits; i++)
{
p = p * d;
}
sum = sum + p;
temp = temp / 10;
}
if (sum == n)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used in this program |
sc | Scanner | Reads the number typed by the user |
n | int | Holds the number entered by the user, kept unchanged for the final comparison |
temp | int | Working copy of n whose last digit is removed in each pass of a loop |
digits | int | Count of the digits in n, used as the power |
sum | int | Running sum of the digits raised to the power digits |
d | int | The last digit of temp in the current pass |
p | int | Holds $d^{digits}$, built by repeated multiplication |
i | int | Loop counter of the for loop that finds the power |
Sample input and output
Sample 1
Enter a number: 153
153 is an Armstrong number.Sample 2
Enter a number: 125
125 is not an Armstrong number.Sample 3
Enter a number: -5
Invalid input. Enter a positive number.Dry run
Take n = 153. The first loop counts the digits, so digits = 3. The second loop then runs with temp = 153 and sum = 0.
| Pass | temp (start) | d | p = $d^3$ | sum | temp (end) |
|---|---|---|---|---|---|
| 1 | 153 | 3 | 27 | 27 | 15 |
| 2 | 15 | 5 | 125 | 152 | 1 |
| 3 | 1 | 1 | 1 | 153 | 0 |
The loop stops as temp is 0. Since sum (153) is equal to n (153), the program displays that 153 is an Armstrong number.
Variations
- Armstrong numbers in a range "Accept two numbers m and n and display all the Armstrong numbers between m and n."
- Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
- Armstrong number with a method "Write a class with a method that checks whether a number is an Armstrong number and returns true or false."