C Sum of Digits Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Sum of Digits?

The sum of digits is the total of every digit in a number after one pass. Peel digits with n % 10 and remove them with n /= 10.

Remember
digitSum(n) = sum of each digit of |n|

Example: 12345
  5 + 4 + 3 + 2 + 1 = 15

One pass only — do not keep summing until one digit
(that is digital root / condense)
nDigitsSum
123451+2+3+4+515
-8028+0+210
000
777

How to Find the Sum of Digits in C

Four clear steps — modulo and integer division do all the work.

  1. Take abs(n) so negatives share the same path.
  2. While n != 0, add n % 10 to a total.
  3. Remove the last digit with n /= 10.
  4. When n becomes 0, return the total.
MethodIdeaTimeBest for
Modulo loop% 10 then / 10O(digits)Interviews, default answer
String digitsConvert to string and sum charsO(digits)Quick hacks (less preferred)
Repeated sumKeep summing until one digitO(digits)Digital root / condense

Pseudocode

Pseudocode
function DigitSum(n):
    n = abs(n)
    total = 0

    while n != 0:
        total = total + (n mod 10)
        n = n / 10

    return total

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Last digitn % 10
Drop digitn /= 10
Stop whenn == 0
Negativesabs(n) first
ZeroDigit sum is 0
vs CondenseOne pass only (not digital root)
Classic check12345 → 15

Live Preview

Enter any integer to see each peeled digit and the final digit sum.

Whole numbers only. Absolute value is used for negatives.

Result
n = 12345
Digits (right → left): 5, 4, 3, 2, 1
Sum = 15

Worked Walkthrough — n = 12345

Peel from the right, add each remainder, then drop that digit.

Calculation
n = 12345, total = 0

12345 % 10 = 5   total = 5    n = 1234
 1234 % 10 = 4   total = 9    n = 123
  123 % 10 = 3   total = 12   n = 12
   12 % 10 = 2   total = 14   n = 1
    1 % 10 = 1   total = 15   n = 0

Stop
Digit sum = 15

C Programs

Start with a fixed number and the classic modulo loop. Then read input, and finish by tracing every peeled digit.

Example 1 — Sum of Digits (Fixed Number)

Use % 10 and / 10 until the number becomes zero.

C
#include <stdio.h>
#include <stdlib.h>

int digit_sum(int number)
{
    number = abs(number);
    int total = 0;

    while (number != 0)
    {
        total += number % 10;
        number /= 10;
    }

    return total;
}

int main(void)
{
    int number = 12345;
    printf("The sum of digits of %d is: %d\n", number, digit_sum(number));
    return 0;
}

How It Works

  1. abs keeps the helper safe for negatives.
  2. Each loop takes the last digit with number % 10.
  3. number /= 10 drops that digit for the next pass.
  4. For 12345 the digits added are 5, 4, 3, 2, then 1.
  5. When number hits 0, return 15.

Example 2 — Sum of Digits (User Input)

Read one integer and reuse the same helper.

C
#include <stdio.h>
#include <stdlib.h>

int digit_sum(int number)
{
    number = abs(number);
    int total = 0;

    while (number != 0)
    {
        total += number % 10;
        number /= 10;
    }

    return total;
}

int main(void)
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);
    printf("Sum of digits: %d\n", digit_sum(number));
    return 0;
}

How It Works

  1. Read one integer with scanf.
  2. Pass it to digit_sum — input and logic stay separate.
  3. Negatives work because the helper uses abs.
  4. Try -802 to confirm the answer is 10.
  5. Same algorithm as Example 1 with a different data source.

Example 3 — Trace Digit Peeling for -802

Print each extracted digit so the right-to-left order is obvious.

C
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int n = -802;
    int x = abs(n);
    int total = 0;
    int digit;

    printf("Starting from abs(%d) = %d\n", n, x);
    while (x != 0)
    {
        digit = x % 10;
        total += digit;
        printf("  take %d, remaining %d, total=%d\n", digit, x / 10, total);
        x /= 10;
    }

    printf("Digit sum: %d\n", total);
    return 0;
}

How It Works

  1. Absolute value turns -802 into 802 before peeling.
  2. Digits arrive as 2, then 0, then 8.
  3. A zero digit still counts as a step but adds nothing.
  4. When remaining is 0, the loop stops.
  5. Final digit sum is 10.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

Digit sum is 0

The loop body never runs when n is already 0, so the total stays 0.

Negatives

Use abs

C % can keep the dividend’s sign. Normalize with abs first for a clean digit sum.

One pass

Do not over-sum

Digit sum stops after one pass. Repeating until one digit is digital root / condense.

Single digit

Answer is itself

For 7, one loop iteration adds 7 and finishes.

Time and Space Complexity

ProgramTimeExtra space
Modulo digit loopO(digits) = O(log n)O(1)
User-input + digit sumO(digits)O(1)
Trace peelO(digits)O(1)

Each digit is visited once. The number of digits grows like log₁₀ n.

Key Takeaways

  • Core tools: n % 10 peels a digit; n /= 10 drops it.
  • One pass: stop when n becomes 0 — that is digit sum, not digital root.
  • Signs: use abs before peeling.
  • Test set: 12345 → 15; −802 → 10; 0 → 0; 7 → 7.

One line: keep adding n % 10 and dividing by 10 until nothing is left.

Frequently Asked Questions

Take the last digit with n % 10, add it to a total, then remove it with n /= 10. Repeat until n becomes 0.
In base 10, the remainder when dividing by 10 is always the rightmost digit (0 through 9).
Use labs(n) or abs(n) first so the sign does not affect the digit sum.
Digit sum is one pass. Condensing (digital root) repeats digit summing until only one digit remains.
O(d), where d is the number of digits — equivalently O(log n) for positive n.

Next: Swap Two Numbers

Exchange two values with a temp variable or arithmetic tricks.

Swap two numbers tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful