ISC Practicals and Theory - Armstrong Number (OOP)
Updated 20 Oct 2026
An Armstrong number is a number that equals the sum of its digits, each raised to the power of the number of digits. This version is written as a class Armstrong with data members and separate methods for accepting the number, counting digits, finding a power, checking and displaying.
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$.
- $9474$ has 4 digits: $9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474$. So 9474 is an Armstrong number.
- $100$ has 3 digits: $1^3 + 0^3 + 0^3 = 1$, which is not $100$. So 100 is not an Armstrong number.
Algorithm
- In
main, accept the numbernumusingScanner. Ifnum <= 0, display an invalid input message and stop. - Create an object
objof classArmstrong; the constructor sets the data membersnanddigitsto 0. - Call
accept(num)to store the number inn. - Call
display(), which callsisArmstrong()and prints the result. isArmstrong()works as follows:- Call
countDigits()and store the result indigits. It copiesnto a local variable and counts how many times it can be divided by 10 before it becomes 0. - Copy
ntotempand setsum = 0. - While
temp > 0: extractd = temp % 10, addpower(d, digits)tosum, then dotemp = temp / 10. - Return
trueifsum == n, otherwisefalse.
- Call
power(b, e)multipliespbybexactlyetimes with aforloop and returns it.
Program
The data members are n and digits. The methods are accept(), countDigits(), power(), isArmstrong() and display(); main only reads the input and creates the object.
import java.util.Scanner;
public class Armstrong
{
int n, digits;
Armstrong()
{
n = 0;
digits = 0;
}
void accept(int num)
{
n = num;
}
int countDigits()
{
int temp = n, c = 0;
while (temp > 0)
{
c++;
temp = temp / 10;
}
return c;
}
int power(int b, int e)
{
int p = 1;
for (int i = 1; i <= e; i++)
{
p = p * b;
}
return p;
}
boolean isArmstrong()
{
digits = countDigits();
int temp = n, sum = 0;
while (temp > 0)
{
int d = temp % 10;
sum = sum + power(d, digits);
temp = temp / 10;
}
return sum == n;
}
void display()
{
if (isArmstrong())
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num <= 0)
{
System.out.println("Invalid input. Enter a positive number.");
}
else
{
Armstrong obj = new Armstrong();
obj.accept(num);
obj.display();
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member holding the number to be checked |
digits | int | Data member holding the number of digits of n, used as the power |
num | int | Parameter of accept() and also the number read in main |
temp | int | Working copy of n whose last digit is removed in each pass (in countDigits() and in isArmstrong()) |
c | int | Counter of digits in countDigits() |
b | int | Parameter of power(): the base |
e | int | Parameter of power(): the exponent |
p | int | Result of power(), built by repeated multiplication |
i | int | Loop counter in power() |
sum | int | Running sum of the digits raised to the power digits |
d | int | The last digit of temp in the current pass |
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
obj | Armstrong | Object of the class, created in main |
Sample input and output
Sample 1
Enter a number: 9474
9474 is an Armstrong number.Sample 2
Enter a number: 100
100 is not an Armstrong number.Sample 3
Enter a number: 0
Invalid input. Enter a positive number.Dry run
Take n = 9474. countDigits() returns 4, so digits = 4. Then isArmstrong() runs with temp = 9474 and sum = 0.
| Pass | temp (start) | d | power(d, 4) | sum | temp (end) |
|---|---|---|---|---|---|
| 1 | 9474 | 4 | 256 | 256 | 947 |
| 2 | 947 | 7 | 2401 | 2657 | 94 |
| 3 | 94 | 4 | 256 | 2913 | 9 |
| 4 | 9 | 9 | 6561 | 9474 | 0 |
The loop stops as temp is 0. sum (9474) equals n (9474), so isArmstrong() returns true and display() prints that 9474 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."
- Using a method that returns boolean "Write a method that accepts an integer and returns true if it is an Armstrong number, otherwise false."