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
- In
main, acceptnum. Ifnum <= 0, display an invalid input message and stop. - Create the object
objand callaccept(num). It storesnuminnand counts the digits intodigitswith a loop. - Call
display(), which comparesarmSum(n)withnand prints the result. 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.
- Base case: if
power(b, e)is recursive:- Base case: if
e == 0, return 1. - Recursive case: return
b * power(b, e - 1).
- Base case: if
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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member holding the number to be checked |
digits | int | Data member holding the number of digits of n, used as the power |
num | int | Parameter of accept() and also the number read in main |
temp | int | Working copy of num in accept() whose last digit is removed while counting digits |
b | int | Parameter of power(): the base |
e | int | Parameter of power(): the exponent, reduced by 1 in every call |
m | int | Parameter of armSum(): the part of the number still to be processed |
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
obj | Armstrong | Object 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.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.
| Call | m | m % 10 | power(m % 10, 3) | Returns |
|---|---|---|---|---|
armSum(370) | 370 | 0 | 0 | 0 + armSum(37) |
armSum(37) | 37 | 7 | 343 | 343 + armSum(3) |
armSum(3) | 3 | 3 | 27 | 27 + 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
- Armstrong numbers in a range "Accept two numbers m and n and display all the Armstrong numbers between m and n."
- Three-digit Armstrong numbers "Display all the Armstrong numbers from 100 to 999."
- Sum of digits recursively "Write a recursive method to find the sum of the digits of a number."