ISC Practicals and Theory - ArmNum Class
Updated 21 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 note shows the program written as a class ArmNum: the number is a data member, a default constructor sets it, and separate methods accept it, count its digits, find a power, check it and display the result, while main creates an object and calls them.
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.
- $100$ has 3 digits: $1^3 + 0^3 + 0^3 = 1$, which is not $100$. So 100 is not an Armstrong number.
- Every single-digit number is an Armstrong number, since $d^1 = d$.
Algorithm
maincreates a Scannerscand an objectobjof classArmNum. The default constructor sets the data membernto 0.maincallsobj.accept(sc). It reads the number intonand returnstrueifn >= 1, otherwisefalse.- If
accept()returnedfalse,mainprints the invalid input message and stops. - Otherwise
maincallsobj.display(), which callsisArm()and prints whethernis an Armstrong number. isArm()works as follows:- Call
count(n)to get the number of digitsk.count()adds 1 tocand dividesxby 10 untilxbecomes 0. - Copy
ntotempand setsum = 0, so thatnitself is not changed. - While
temp > 0: take the last digitd = temp % 10, addpower(d, k)tosum, then dotemp = temp / 10. - Return
trueifsum == n, otherwisefalse.
- Call
power(b, e)multipliespbybexactlyetimes in aforloop, starting withp = 1, and returnsp. It does not useMath.pow.
Program
The data member is n. The methods are accept(), count(), power(), isArm() and display(), each doing one job; main only creates the object and calls them.
import java.util.Scanner;
/* Class ArmNum: checks whether a number is an Armstrong number
(sum of its digits, each raised to the number of digits, equals the number). */
public class ArmNum
{
int n; // data member: the number to be checked
/* Default constructor: sets the number to 0 (not yet accepted). */
ArmNum()
{
n = 0;
}
/* Reads the number into n using the given Scanner.
Returns true if n >= 1 (valid), otherwise false. */
boolean accept(Scanner sc)
{
System.out.print("Enter a number: ");
n = sc.nextInt();
return n >= 1;
}
/* Counts the digits of x.
Parameter x: the number whose digits are counted. Returns the count. */
int count(int x)
{
int c = 0; // number of digits counted so far
while (x > 0)
{
c++; // one more digit
x = x / 10; // remove the last digit
}
return c;
}
/* Finds b raised to the power e by repeated multiplication.
Parameters: b is the base, e is the exponent. Returns b^e. */
int power(int b, int e)
{
int p = 1; // result, starts at 1
for (int i = 1; i <= e; i++)
{
p = p * b; // multiply by the base e times
}
return p;
}
/* Checks whether n is an Armstrong number.
Returns true if the sum of digit^count equals n, otherwise false. */
boolean isArm()
{
int k = count(n); // number of digits of n, used as the power
int temp = n; // working copy, so that n is not destroyed
int sum = 0; // sum of the digits raised to the power k
while (temp > 0)
{
int d = temp % 10; // last digit
sum = sum + power(d, k);
temp = temp / 10; // drop the last digit
}
return sum == n;
}
/* Prints whether n is an Armstrong number or not. */
void display()
{
if (isArm())
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
/* main: creates an object of ArmNum and calls its methods. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the input
ArmNum obj = new ArmNum(); // object of the class
if (obj.accept(sc))
{
obj.display();
}
else
{
System.out.println("Invalid input. Enter a number greater than 0.");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member holding the number to be checked |
sc | Scanner | Reads the number typed by the user; created in main and passed to accept() |
x | int | Parameter of count(): the number whose digits are counted; its last digit is removed in each pass |
c | int | Counter of digits in count() |
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() |
k | int | Number of digits of n, used as the power in isArm() |
temp | int | Working copy of n whose last digit is removed in each pass |
sum | int | Running sum of the digits raised to the power k |
d | int | The last digit of temp in the current pass |
obj | ArmNum | Object of the class, created in main |
args | String[] | Command line arguments of main; not used |
Sample input and output
Sample 1
Enter a number: 153
153 is an Armstrong number.Sample 2
Enter a number: 100
100 is not an Armstrong number.Sample 3
Enter a number: 7
7 is an Armstrong number.Sample 4
Enter a number: 0
Invalid input. Enter a number greater than 0.Dry run
Take n = 153. count(153) returns 3, so k = 3. Then isArm() runs with temp = 153 and sum = 0.
| Pass | temp (start) | d | power(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. sum (153) equals n (153), so isArm() returns true and display() prints 153 is an Armstrong number., as in the first sample run.
Variations
- Armstrong numbers in a range "Accept two numbers m and n, where 1 <= m <= n, and display all the Armstrong numbers from m to n."
- Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
- Armstrong Number The same check written as a single class with other methods is in Armstrong Number.