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

ISC Practicals and Theory - Armstrong Number

Updated 20 Oct 2026

An Armstrong number is a number that equals the sum of its own digits, each raised to the power of the number of digits. This program accepts a number, counts its digits, adds up the powers and compares the sum with the number, all inside main.

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.
  • $125$ has 3 digits: $1^3 + 2^3 + 5^3 = 1 + 8 + 125 = 134$, which is not $125$. So 125 is not an Armstrong number.

Algorithm

  1. Accept the number n using Scanner.
  2. If n <= 0, display an invalid input message and stop.
  3. Count the digits of n:
    • Copy n to temp and set digits = 0.
    • While temp > 0, increase digits by 1 and do temp = temp / 10.
  4. Copy n to temp again and set sum = 0.
  5. While temp > 0:
    • Extract the last digit: d = temp % 10.
    • Find $d^{digits}$ in p by multiplying p by d a total of digits times, using a for loop.
    • Add p to sum.
    • Remove the last digit: temp = temp / 10.
  6. If sum == n, display that it is an Armstrong number, otherwise display that it is not.

Program

The whole logic is written in the main method of class Armstrong.

import java.util.Scanner;
public class Armstrong
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        if (n <= 0)
        {
            System.out.println("Invalid input. Enter a positive number.");
        }
        else
        {
            int temp = n, digits = 0;
            while (temp > 0)
            {
                digits++;
                temp = temp / 10;
            }
            temp = n;
            int sum = 0;
            while (temp > 0)
            {
                int d = temp % 10;
                int p = 1;
                for (int i = 1; i <= digits; i++)
                {
                    p = p * d;
                }
                sum = sum + p;
                temp = temp / 10;
            }
            if (sum == n)
                System.out.println(n + " is an Armstrong number.");
            else
                System.out.println(n + " is not an Armstrong number.");
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used in this program
scScannerReads the number typed by the user
nintHolds the number entered by the user, kept unchanged for the final comparison
tempintWorking copy of n whose last digit is removed in each pass of a loop
digitsintCount of the digits in n, used as the power
sumintRunning sum of the digits raised to the power digits
dintThe last digit of temp in the current pass
pintHolds $d^{digits}$, built by repeated multiplication
iintLoop counter of the for loop that finds the power

Sample input and output

Sample 1

Enter a number: 153
153 is an Armstrong number.

Sample 2

Enter a number: 125
125 is not an Armstrong number.

Sample 3

Enter a number: -5
Invalid input. Enter a positive number.

One value per line

Dry run

Take n = 153. The first loop counts the digits, so digits = 3. The second loop then runs with temp = 153 and sum = 0.

Passtemp (start)dp = $d^3$sumtemp (end)
11533272715
21551251521
31111530

The loop stops as temp is 0. Since sum (153) is equal to n (153), the program displays that 153 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. Armstrong number with a method "Write a class with a method that checks whether a number is an Armstrong number and returns true or false."