---
title: "Sum of digits using recursion"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:sum-digits-recursive"
source: https://www.bctengineer.com/q/semester_1/computer_programming/sum-digits-recursive
site: BCT Engineering
---

# Sum of digits using recursion

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2072 Chaitra, 4 marks
- Video: [06 Recursion | How to calculate sum of digits of a number using recursion in C | by Sanjay Gupta](https://www.youtube.com/watch?v=1nhvBtIOO2w) — 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/sum-digits-recursive
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write a program to calculate sum of digits of a given 5-digits number entered by the user using recursive function.

## Short answer

Recursive sumDigits: base case n==0 returns 0; otherwise returns (n%10) + sumDigits(n/10). For input 12345, output is 15.

## Answer

**Idea.** The last digit is `n % 10` and the remaining digits are `n / 10`. So

sumDigits(n) = (n % 10) + sumDigits(n / 10), and sumDigits(0) = 0 (base case).

Trace for 12345: 5 + sumDigits(1234) = 5 + 4 + sumDigits(123) = 5 + 4 + 3 + 2 + 1 = 15.

```c
#include <stdio.h>

int sumDigits(int n)
{
    if (n == 0)                      /* base case */
        return 0;
    return (n % 10) + sumDigits(n / 10);
}

int main(void)
{
    int n;
    printf("Enter a 5-digit number: ");
    scanf("%d", &n);
    if (n < 10000 || n > 99999)
        printf("Not a 5-digit number\n");
    else
        printf("Sum of digits = %d\n", sumDigits(n));
    return 0;
}
```

**Sample run**

Input: `12345`

```text
Enter a 5-digit number: Sum of digits = 15
```

## Check yourself

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

1. In sumDigits(n), what do n % 10 and n / 10 give?
   A. The remaining digits and the last digit
   B. The first digit and the last digit
   C. The last digit and the remaining digits
   D. The digit count and the sum
   Answer: C. n % 10 is the last digit, and integer division n / 10 removes it. (Key point: Splitting the number)

2. What stops the recursion in sumDigits?
   A. n == 0 returns 0
   B. n == 1 returns 1
   C. n < 10 returns n/10
   D. n % 10 == 0 returns 0
   Answer: A. When no digits remain, n is 0 and nothing more is added. (Key point: Base case)

3. What is sumDigits(12345)?
   A. 12345
   B. 5
   C. 15
   D. 54321
   Answer: C. 5 + 4 + 3 + 2 + 1 = 15. (Key point: Trace)

## Related in Computer Programming

- [Sum of digits until single digit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adigit-sum-single-digit) — Computer Programming, chapter 5, asked 2×
- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Complex number sum via functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-sum-function) — Computer Programming, chapter 8
- [Armstrong number using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarmstrong-recursive) — Computer Programming, chapter 6
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6

## Related in other subjects

- [Sum of digits processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-sum-of-digits) — Embedded System, chapter 2
- [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
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
