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

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

  1. In main, accept the number num using Scanner. If num <= 0, display an invalid input message and stop.
  2. Create an object obj of class Armstrong; the constructor sets the data members n and digits to 0.
  3. Call accept(num) to store the number in n.
  4. Call display(), which calls isArmstrong() and prints the result.
  5. isArmstrong() works as follows:
    • Call countDigits() and store the result in digits. It copies n to a local variable and counts how many times it can be divided by 10 before it becomes 0.
    • Copy n to temp and set sum = 0.
    • While temp > 0: extract d = temp % 10, add power(d, digits) 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 with a for loop 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

VariableData TypePurpose
nintData member holding the number to be checked
digitsintData member holding the number of digits of n, used as the power
numintParameter of accept() and also the number read in main
tempintWorking copy of n whose last digit is removed in each pass (in countDigits() and in isArmstrong())
cintCounter of digits in countDigits()
bintParameter of power(): the base
eintParameter of power(): the exponent
pintResult of power(), built by repeated multiplication
iintLoop counter in power()
sumintRunning sum of the digits raised to the power digits
dintThe last digit of temp in the current pass
argsString[]Command line arguments of main; not used
scScannerReads the number typed by the user
objArmstrongObject 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.

One value per line

Dry run

Take n = 9474. countDigits() returns 4, so digits = 4. Then isArmstrong() runs with temp = 9474 and sum = 0.

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

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

  1. Armstrong numbers in a range "Accept two numbers m and n and display all the Armstrong numbers between m and n."
  2. Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
  3. Using a method that returns boolean "Write a method that accepts an integer and returns true if it is an Armstrong number, otherwise false."