---
title: "Sum of digits until single digit"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:digit-sum-single-digit"
source: https://www.bctengineer.com/q/semester_1/computer_programming/digit-sum-single-digit
site: BCT Engineering
---

# Sum of digits until single digit

Computer Programming · Chapter 5: Control statements

- Asked in: 2068 Chaitra, 6 marks; 2071 Shrawan, 4 marks (asked 2 times)
- Video: [55 - Sum Of Digits of Number till Single Digit](https://www.youtube.com/watch?v=zUnEUwyVCz8) — Code Semantic
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/digit-sum-single-digit
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a program in C that calculates the sum of digits entered by the user successively until the sum reduces to a single digit number. For example, 12345 => 1+2+3+4+5=15=>1+5=6. (Find out sum of digit of number until the number becomes one digit number [891>18>9].)

## Short answer

Repeatedly replace the number by the sum of its digits (using n % 10 and n /= 10) until it becomes a single digit. For 12345, the intermediate sum is 15, then the final single digit is 6.

## Answer

Logic: repeatedly extract each digit with `n % 10`, add it, and remove it with `n / 10`. Take the sum as the new number and repeat until the number is below 10 (a single digit).

```c
#include <stdio.h>
int main(void)
{
    long n;
    int sum;
    printf("Enter a number: ");
    scanf("%ld", &n);
    while (n > 9)
    {
        sum = 0;
        while (n > 0)
        {
            sum += n % 10;
            n /= 10;
        }
        printf("Sum of digits = %d\n", sum);
        n = sum;
    }
    printf("Single digit = %ld\n", n);
    return 0;
}
```
Output for 12345:
```text
Enter a number: Sum of digits = 15
Sum of digits = 6
Single digit = 6
```
Output for 891:
```text
Enter a number: Sum of digits = 18
Sum of digits = 9
Single digit = 9
```

#### Explanation
- The inner `while (n > 0)` loop adds the digits: 1+2+3+4+5 = 15.
- The outer loop `while (n > 9)` repeats with n = 15: 1+5 = 6, and 6 is a single digit, so it stops.

## Check yourself

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

1. How does the program get the last digit of n and remove it?
   A. n % 10 gives the digit, n / 10 removes it
   B. n / 10 gives the digit, n % 10 removes it
   C. n % 2 gives the digit, n / 2 removes it
   D. n - 10 gives the digit, n + 10 removes it
   Answer: A. Remainder by 10 is the last digit and integer division by 10 drops it. (Key point: Digit extraction)

2. Why does the outer loop use while (n > 9)?
   A. It stops when the number becomes 0
   B. It repeats until the number is a single digit
   C. It runs exactly twice
   D. It checks that n is positive only
   Answer: B. A number below 10 is one digit, so the process stops there. (Key point: Outer loop condition)

3. Why is sum set to 0 inside the outer loop before the inner loop?
   A. To make the input number zero
   B. To avoid reading input again
   C. Each new round must start adding from zero
   D. Because sum is a long variable
   Answer: C. Without the reset, digits from earlier rounds would add into the new sum. (Key point: Reset sum)

4. For input 891, what sums are printed?
   A. 9 then 18
   B. 18 only
   C. 27 then 9
   D. 18 then 9
   Answer: D. 8+9+1 = 18, then 1+8 = 9, a single digit. (Key point: Trace)

## Related in Computer Programming

- [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
- [Sum of digits using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-digits-recursive) — Computer Programming, chapter 6
- [Complex number sum via functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-complex-sum-function) — Computer Programming, chapter 8
- [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
- [Count even and odd until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-even-odd-until-minus1) — Computer Programming, chapter 5
- [Count prime and composite until -1](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acount-prime-composite-until-minus1) — Computer Programming, chapter 5

## 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
- [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
- [Poker test, 1000 four-digit numbers (2069)](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Apoker-test-numerical-2069-bhadra) — Simulation and Modeling, chapter 6, asked 3×
- [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
