Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main creates a Scanner sc and reads the range: m and last (the value n). If m < 1 or m > last, it prints the invalid range message and stops.
  2. Otherwise it prints the heading and sets found = 0.
  3. A for loop runs i from m to last, both included. In every pass:
    • Create a new object obj = new ArmNum(i). The parameterised constructor stores i in the data member n.
    • Call obj.isArm(). If it returns true, call obj.display() to print the number and add 1 to found.
  4. After the loop, if found is 0, print None. Then print a new line.
  5. isArm() works as follows:
    • Call count(n) to get the number of digits k.
    • Copy n to temp and set sum = 0.
    • While temp > 0: take the last digit d = temp % 10, add power(d, k) to sum, then do temp = temp / 10.
    • Return true if sum == n, otherwise false.
  6. power(b, e) multiplies p by b exactly e times, without Math.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

VariableData TypePurpose
nintData member holding the number to be checked
numintParameter of the constructor: the number stored in n
xintParameter of count(): the number whose digits are counted; its last digit is removed in each pass
cintCounter of digits in count()
bintParameter of power(): the base
eintParameter of power(): the exponent
pintResult of power(), built by repeated multiplication
kintNumber of digits of n, used as the power in isArm()
tempintWorking copy of n whose last digit is removed in each pass
sumintRunning sum of the digits raised to the power k
dintThe last digit of temp in the current pass
scScannerReads the range typed by the user
mintStart of the range, read in main
lastintEnd of the range (the number n of the question), read in main
foundintCount of Armstrong numbers displayed
iintLoop counter: in power() it counts the multiplications, in main it is the number being tested
objArmNumNew object created for each number i in the range
argsString[]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:
None

Sample 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.

One value per line

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.

Passtemp (start)dpower(d, 4)sumtemp (end)
194744256256947
294772401265794
394425629139
499656194740

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

  1. Armstrong number "Accept a number and check whether it is an Armstrong number." See Armstrong Number.
  2. Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
  3. Count of Armstrong numbers "Display the Armstrong numbers from m to n and also the count of such numbers."