Sum of Digits of a Number in C – Loop and Recursion

Finding the sum of digits of a number in C is a classic programming exercise that teaches digit extraction using the modulo operator. You extract each digit by taking n % 10, add it to a running total, then divide n by 10 to drop the last digit. Repeat until no digits remain. This technique appears in number theory problems — Armstrong numbers, Kaprekar constants, and digital roots all rely on the same digit-extraction loop.

How It Works — Step by Step

Given the number 12345, here is what the loop does each iteration:

  1. 12345 % 10 = 5 → sum = 5, n becomes 1234
  2. 1234 % 10 = 4 → sum = 9, n becomes 123
  3. 123 % 10 = 3 → sum = 12, n becomes 12
  4. 12 % 10 = 2 → sum = 14, n becomes 1
  5. 1 % 10 = 1 → sum = 15, n becomes 0 — loop ends

Result: 1 + 2 + 3 + 4 + 5 = 15

Program 1 — Sum of Digits Using a Loop

/* Sum of digits of a number in C — iterative
 * Compile: gcc -ansi -Wall -Wextra sumdigits.c -o sumdigits */
#include <stdio.h>

int sum_of_digits(long n)
{
    int sum = 0;
    if (n < 0)
        n = -n;
    while (n > 0) {
        sum += (int)(n % 10);
        n /= 10;
    }
    return sum;
}

int main(void)
{
    long num;

    printf("Enter a number: ");
    scanf("%ld", &num);

    printf("Number          = %ld\n", num);
    printf("Sum of digits   = %d\n", sum_of_digits(num));

    return 0;
}

Program 2 — Sum of Digits Using Recursion

/* Sum of digits of a number in C — recursive
 * Compile: gcc -ansi -Wall -Wextra sumdigits_rec.c -o sumdigits_rec */
#include <stdio.h>

int sum_recursive(long n)
{
    if (n < 0)
        n = -n;
    if (n == 0)
        return 0;
    return (int)(n % 10) + sum_recursive(n / 10);
}

int main(void)
{
    long num;

    printf("Enter a number: ");
    scanf("%ld", &num);

    printf("Number        = %ld\n", num);
    printf("Sum of digits = %d\n", sum_recursive(num));

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra sumdigits.c -o sumdigits
./sumdigits

Sample Input and Output

Test 1:

Enter a number: 12345
Number          = 12345
Sum of digits   = 15

Test 2:

Enter a number: 9999
Number          = 9999
Sum of digits   = 36

Test 3 (single non-zero digit in hundreds):

Enter a number: 100
Number          = 100
Sum of digits   = 1

Code Explanation

  • n % 10 — extracts the last (units) digit of n. For 12345 this gives 5.
  • n /= 10 — integer division drops the last digit. 12345 becomes 1234.
  • Negative handlingif (n < 0) n = -n converts a negative input to positive before processing. The digit sum of -123 equals the digit sum of 123.
  • Recursive version — each call contributes the units digit and recurses on the remaining number. The base case n == 0 stops the recursion.
  • Why long — using long for the input handles numbers up to roughly 2 billion, avoiding overflow for large inputs like 999999999.

What This Program Teaches

  • Modulo for digit extraction% 10 is the standard C idiom for pulling off the rightmost digit.
  • Integer division for shifting/ 10 moves to the next digit position; combining with modulo walks through every digit.
  • Recursion with a numeric base case — the recursive version shows how to decompose a number into its last digit plus the remaining number.
  • Function decomposition — placing the logic in sum_of_digits() makes it reusable: Armstrong number checkers, strong number checkers, and digital root calculators all call the same function.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>