ISC Practicals and Theory - ArmNum Class (OOP)
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 version displays all the Armstrong numbers from m to n using the class ArmNum: it has a parameterised constructor ArmNum(int num), and main creates a **new ArmNum object for every number** in the range m to n.
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$.
- $371$ has 3 digits: $3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371$. So 371 is an Armstrong number.
- $372$: $3^3 + 7^3 + 2^3 = 27 + 343 + 8 = 378$, which is not $372$. So 372 is not an Armstrong number.
- Every single-digit number is an Armstrong number, since $d^1 = d$.
Algorithm
maincreates a Scannerscand reads the range:mandlast(the value n). Ifm < 1orm > last, it prints the invalid range message and stops.- Otherwise it prints the heading and sets
found = 0. - A
forloop runsifrommtolast, both included. In every pass:- Create a new object
obj = new ArmNum(i). The parameterised constructor storesiin the data membern. - Call
obj.isArm(). If it returnstrue, callobj.display()to print the number and add 1 tofound.
- Create a new object
- After the loop, if
foundis 0, printNone. Then print a new line. isArm()works as follows:- Call
count(n)to get the number of digitsk. - Copy
ntotempand setsum = 0. - 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, withoutMath.pow.
Program
The data member is n. The parameterised constructor ArmNum(int num) sets it, count(), power() and isArm() do the checking, and display() prints one number. The range loop is in main, which creates one object per number.
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).
An object is created for every number in the range m to n. */
public class ArmNum
{
int n; // data member: the number to be checked
/* Parameterised constructor: stores the number num in n. */
ArmNum(int num)
{
n = num;
}
/* 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 n followed by a space (used for each Armstrong number found). */
void display()
{
System.out.print(n + " ");
}
/* main: reads the range m to last and creates a new ArmNum object
for every number in the range. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the input
System.out.print("Enter m: ");
int m = sc.nextInt(); // start of the range
System.out.print("Enter n: ");
int last = sc.nextInt(); // end of the range (n)
if (m < 1 || m > last)
{
System.out.println("Invalid range. Enter 1 <= m <= n.");
}
else
{
System.out.println("Armstrong numbers from " + m + " to " + last + ":");
int found = 0; // how many were found
for (int i = m; i <= last; i++)
{
ArmNum obj = new ArmNum(i); // new object for each number
if (obj.isArm())
{
obj.display();
found++;
}
}
if (found == 0)
System.out.print("None");
System.out.println();
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member holding the number to be checked |
num | int | Parameter of the constructor: the number stored in n |
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 |
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 |
sc | Scanner | Reads the range typed by the user |
m | int | Start of the range, read in main |
last | int | End of the range (the number n of the question), read in main |
found | int | Count of Armstrong numbers displayed |
i | int | Loop counter: in power() it counts the multiplications, in main it is the number being tested |
obj | ArmNum | New object created for each number i in the range |
args | String[] | Command line arguments of main; not used |
Sample input and output
Sample 1
Enter m: 100
Enter n: 500
Armstrong numbers from 100 to 500:
153 370 371 407 Sample 2
Enter m: 200
Enter n: 300
Armstrong numbers from 200 to 300:
NoneSample 3
Enter m: 9474
Enter n: 9474
Armstrong numbers from 9474 to 9474:
9474 Sample 4
Enter m: 50
Enter n: 10
Invalid range. Enter 1 <= m <= n.Dry run
Take m = 9474 and last = 9474. The loop runs once with i = 9474, so obj = new ArmNum(9474) and n = 9474. count(9474) returns 4, so k = 4. Then isArm() 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 |
sum (9474) equals n (9474), so isArm() returns true, display() prints 9474 and found becomes 1. The loop ends, found is not 0, so None is not printed, and the output is 9474 on one line, as in the third sample run.
Variations
- Armstrong number "Accept a number and check whether it is an Armstrong number." See Armstrong Number.
- Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
- Count of Armstrong numbers "Display the Armstrong numbers from m to n and also the count of such numbers."