0
0
CProgramBeginner · 2 min read

C Program to Find Sum of Digits of a Number

A C program to find the sum of digits uses a loop to extract each digit using num % 10, adds it to a sum, and reduces the number by num / 10 until the number is zero.
📋

Examples

Input1234
OutputSum of digits = 10
Input0
OutputSum of digits = 0
Input99999
OutputSum of digits = 45
🧠

How to Think About It

To find the sum of digits, repeatedly take the last digit of the number by using num % 10, add it to a running total, then remove that digit by dividing the number by 10 using num / 10. Repeat until the number becomes zero.
📐

Algorithm

1
Get input number from the user
2
Initialize sum to 0
3
While number is greater than 0:
4
Extract last digit using modulo 10
5
Add digit to sum
6
Remove last digit by dividing number by 10
7
Print the sum
💻

Code

c
#include <stdio.h>

int main() {
    int num, sum = 0, digit;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num == 0) {
        printf("Sum of digits = 0\n");
        return 0;
    }
    while (num > 0) {
        digit = num % 10;
        sum += digit;
        num /= 10;
    }
    printf("Sum of digits = %d\n", sum);
    return 0;
}
Output
Enter a number: 1234 Sum of digits = 10
🔍

Dry Run

Let's trace the input 1234 through the code

1

Initial input

num = 1234, sum = 0

2

First iteration

digit = 1234 % 10 = 4, sum = 0 + 4 = 4, num = 1234 / 10 = 123

3

Second iteration

digit = 123 % 10 = 3, sum = 4 + 3 = 7, num = 123 / 10 = 12

4

Third iteration

digit = 12 % 10 = 2, sum = 7 + 2 = 9, num = 12 / 10 = 1

5

Fourth iteration

digit = 1 % 10 = 1, sum = 9 + 1 = 10, num = 1 / 10 = 0

6

Loop ends

num is 0, exit loop, sum = 10

Iterationnumdigitsum
1123444
212337
31229
41110
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, which we add to the sum.

Step 2: Remove last digit

Dividing the number by 10 with num /= 10 removes the last digit, preparing for the next iteration.

Step 3: Repeat until done

We repeat this process until the number becomes zero, ensuring all digits are added.

🔄

Alternative Approaches

Using recursion
c
#include <stdio.h>

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

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Sum of digits = %d\n", sumDigits(num));
    return 0;
}
Recursion is elegant but uses more memory due to function calls.
Using string conversion
c
#include <stdio.h>
#include <string.h>

int main() {
    char numStr[20];
    int sum = 0;
    printf("Enter a number: ");
    scanf("%s", numStr);
    for (int i = 0; i < strlen(numStr); i++) {
        sum += numStr[i] - '0';
    }
    printf("Sum of digits = %d\n", sum);
    return 0;
}
Converting to string simplifies digit access but uses extra space.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time depends on the number of digits d.

Space Complexity

Only a few variables are used, so space is constant O(1).

Which Approach is Fastest?

The iterative method is fastest and uses least memory; recursion adds overhead, and string conversion uses extra space.

ApproachTimeSpaceBest For
Iterative loopO(d)O(1)Simple and efficient
RecursionO(d)O(d)Elegant but uses more memory
String conversionO(d)O(d)Easy digit access but extra space
💡
Always initialize your sum variable to zero before starting the loop.
⚠️
Forgetting to update the number inside the loop causes an infinite loop.