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

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

  1. main creates a Scanner sc and an object obj of class ArmNum. The default constructor sets the data member n to 0.
  2. main calls obj.accept(sc). It reads the number into n and returns true if n >= 1, otherwise false.
  3. If accept() returned false, main prints the invalid input message and stops.
  4. Otherwise main calls obj.display(), which calls isArm() and prints whether n is an Armstrong number.
  5. isArm() works as follows:
    • Call count(n) to get the number of digits k. count() adds 1 to c and divides x by 10 until x becomes 0.
    • Copy n to temp and set sum = 0, so that n itself is not changed.
    • 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 in a for loop, starting with p = 1, and returns p. It does not use Math.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

VariableData TypePurpose
nintData member holding the number to be checked
scScannerReads the number typed by the user; created in main and passed to accept()
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
iintLoop counter in power()
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
objArmNumObject of the class, created in main
argsString[]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.

One value per line

Dry run

Take n = 153. count(153) returns 3, so k = 3. Then isArm() runs with temp = 153 and sum = 0.

Passtemp (start)dpower(d, 3)sumtemp (end)
11533272715
21551251521
31111530

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

  1. 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."
  2. Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
  3. Armstrong Number The same check written as a single class with other methods is in Armstrong Number.