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

ISC Practicals and Theory - Armstrong Number (Recursive Technique)

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. In this version the power and the sum of the powers of the digits are both found by methods that call themselves, which is the recursive technique.

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

  • $370$ has 3 digits: $3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370$. So 370 is an Armstrong number.
  • $123$ has 3 digits: $1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36$, which is not $123$. So 123 is not an Armstrong number.

Algorithm

  1. In main, accept num. If num <= 0, display an invalid input message and stop.
  2. Create the object obj and call accept(num). It stores num in n and counts the digits into digits with a loop.
  3. Call display(), which compares armSum(n) with n and prints the result.
  4. armSum(m) is recursive:
    • Base case: if m == 0, return 0.
    • Recursive case: return power(m % 10, digits) + armSum(m / 10). That is, the last digit's power plus the same answer for the number without its last digit.
  5. power(b, e) is recursive:
    • Base case: if e == 0, return 1.
    • Recursive case: return b * power(b, e - 1).

Program

The methods power() and armSum() call themselves. The data members are n and digits.

import java.util.Scanner;
public class Armstrong
{
    int n, digits;
    Armstrong()
    {
        n = 0;
        digits = 0;
    }
    void accept(int num)
    {
        n = num;
        digits = 0;
        int temp = num;
        while (temp > 0)
        {
            digits++;
            temp = temp / 10;
        }
    }
    int power(int b, int e)
    {
        if (e == 0)
            return 1;
        return b * power(b, e - 1);
    }
    int armSum(int m)
    {
        if (m == 0)
            return 0;
        return power(m % 10, digits) + armSum(m / 10);
    }
    void display()
    {
        if (armSum(n) == n)
            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 num in accept() whose last digit is removed while counting digits
bintParameter of power(): the base
eintParameter of power(): the exponent, reduced by 1 in every call
mintParameter of armSum(): the part of the number still to be processed
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: 370
370 is an Armstrong number.

Sample 2

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

Sample 3

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

One value per line

Dry run

Take n = 370. accept() sets digits = 3. The calls of armSum() go down until m is 0, then the values are added on the way back.

Callmm % 10power(m % 10, 3)Returns
armSum(370)370000 + armSum(37)
armSum(37)377343343 + armSum(3)
armSum(3)332727 + armSum(0)
armSum(0)0--0

Going back: armSum(3) = 27, armSum(37) = 343 + 27 = 370, armSum(370) = 0 + 370 = 370. Since this equals n, the program displays that 370 is an Armstrong number.

Inside power(7, 3) the calls are $7 \times$ power(7, 2), then $7 \times$ power(7, 1), then $7 \times$ power(7, 0), and power(7, 0) returns 1. So the result is $7 \times 7 \times 7 = 343$.

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. Sum of digits recursively "Write a recursive method to find the sum of the digits of a number."