---
title: "Armstrong number using recursion"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:armstrong-recursive"
source: https://www.bctengineer.com/q/semester_1/computer_programming/armstrong-recursive
site: BCT Engineering
---

# Armstrong number using recursion

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2073 Shrawan, 5 marks
- Video: [11 Recursion | How to check whether the number is armstrong or not using recursion in C](https://www.youtube.com/watch?v=OW5R9yLnxzE) — Sanjay Gupta | Cloud & AI
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/armstrong-recursive
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write a program to check whether a given number is Armstrong number or not using recursive function.

## Short answer

To check if a number n is Armstrong, count its digits recursively, then compute sum of each digit raised to that count recursively. For n=153, digits count 3, 1³+5³+3³=153, so output is Armstrong. The program uses count_digits and power_sum recursive functions.

## Answer

An Armstrong number of d digits equals the sum of the d-th powers of its digits (153 = 1³ + 5³ + 3³ = 153). The recursive function `power_sum(n, d)` returns the last digit raised to d plus `power_sum(n/10, d)`, stopping when `n` becomes 0. Another recursive function, `count_digits`, finds d.

```c
#include <stdio.h>
#include <math.h>
int count_digits(int n)
{
    if (n == 0) return 0;
    return 1 + count_digits(n / 10);
}
int power_sum(int n, int d)       /* sum of d-th powers of digits, recursive */
{
    if (n == 0) return 0;
    return (int)(pow(n % 10, d) + 0.5) + power_sum(n / 10, d);
}
int is_armstrong(int n)
{
    return n == power_sum(n, count_digits(n));
}
int main(void)
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (is_armstrong(n))
        printf("%d is an Armstrong number\n", n);
    else
        printf("%d is not an Armstrong number\n", n);
    return 0;
}
```
Output for 153:
```text
Enter a number: 153 is an Armstrong number
```

Another test: 9474 (= 9⁴ + 4⁴ + 7⁴ + 4⁴) is also an Armstrong number.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. A d-digit Armstrong number equals which quantity?
   A. The sum of its digits
   B. The sum of the d-th powers of its digits
   C. The product of its digits raised to d
   D. The sum of the squares of its digits
   Answer: B. It is the sum of each digit raised to the power d, the number of digits (153 = 1^3 + 5^3 + 3^3). (Key point: Armstrong definition)

2. What does power_sum(n, d) return when n is not 0?
   A. (n/10)^d + power_sum(n%10, d)
   B. n^d + power_sum(n-1, d)
   C. (last digit)^d * power_sum(n/10, d)
   D. (last digit)^d + power_sum(n/10, d)
   Answer: D. n%10 is the last digit, raised to d and added to the result for the remaining digits n/10. (Key point: Recursive step of power_sum)

3. What is the base case of count_digits and power_sum?
   A. n == 1, returning 1
   B. n == 0, returning 0
   C. n == 10, returning 0
   D. n < 0, returning -1
   Answer: B. Once all digits are removed n becomes 0 and 0 is returned, which ends the recursion. (Key point: Base case)

4. How does is_armstrong(n) decide the result?
   A. It compares n with count_digits(n)
   B. It checks whether n % 10 equals d
   C. It compares power_sum(n, 3) with n for every n
   D. It compares n with power_sum(n, count_digits(n))
   Answer: D. The digit count d is found first, then the d-th power sum is compared with n itself. (Key point: Putting the pieces together)

## Related in Computer Programming

- [Check triangular number](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Atriangular-number) — Computer Programming, chapter 5
- [Recursive function and how it works](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Arecursive-function-explain) — Computer Programming, chapter 6, asked 3×
- [Algorithm and flowchart help; Armstrong flowchart](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-flowchart-armstrong) — Computer Programming, chapter 2
- [Armstrong numbers in a range](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarmstrong-range-function) — Computer Programming, chapter 6
- [nth Fibonacci term prime check](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afibonacci-nth-prime) — Computer Programming, chapter 6
- [Sum of digits using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-digits-recursive) — Computer Programming, chapter 6

## Related in other subjects

- [Is recursion slow; recursive vs non-recursive](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Arecursion-slow-compare) — Data Structure and Algorithm, chapter 5
- [Recursion; iterative vs recursive choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adefine-recursion-iterative-vs-recursive) — Data Structure and Algorithm, chapter 5
- [Normal vs recursive function](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Anormal-vs-recursive-function) — Data Structure and Algorithm, chapter 5
- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
