Sum of digits in C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Summing the digits of a number is a small problem, but it is a good exercise in integer arithmetic, loops, and input handling. In C#, the standard solution is to peel off digits with modulus and integer division until the number is exhausted.
The Arithmetic Approach
For a number like 472, the last digit is 472 % 10, which gives 2. Then 472 / 10 gives 47, and you repeat the process until the value becomes zero.
This leads to a compact implementation:
This is usually the best answer because it is efficient, does not allocate strings, and works directly with the numeric representation.
Handle Zero and Negative Values Correctly
One subtle point is 0. The loop above returns 0, which is correct, but only because the initial sum starts at zero. Negative numbers need normalization with Math.Abs, otherwise the modulus results can be harder to reason about.
If you want to make the behavior explicit:
That version reads clearly when edge-case behavior matters.
A String-Based Alternative
If readability matters more than raw efficiency, converting the number to a string is also valid:
This is easy to understand, but it creates a string and iterates over characters, so it does more work than the arithmetic version.
For interview questions or tight loops, prefer the arithmetic solution. For quick application code, either version may be fine.
Recursive Version
You can also write the logic recursively:
This is elegant for small examples, but iteration is usually the better default in C# because it avoids stack growth and keeps control flow straightforward.
When This Pattern Is Useful
The same digit-peeling technique appears in several related tasks:
- checking whether a number is a palindrome
- computing a digital root
- counting digits
- validating checksum-like logic in simple exercises
Once you understand % 10 and / 10, many introductory number-manipulation problems become easier.
Common Pitfalls
The most common mistake is forgetting to update the number inside the loop. If you never divide by 10, the loop never ends.
Another issue is mishandling negative values. If you do not normalize the sign first, the intermediate digits may be negative, which changes the sum.
Developers also sometimes reach for string conversion by default without realizing the arithmetic version is simpler and more direct for this exact problem.
Finally, if you are using the recursive approach, remember that it is mainly for clarity or teaching. For ordinary production code, the iterative loop is usually better.
Summary
- The standard C# solution uses
% 10to extract digits and/ 10to remove them. - Normalize negative numbers with
Math.Abs. - The iterative version is usually the clearest and most efficient.
- A string-based version is readable but allocates more.
- The same technique applies to many other digit-processing problems.
Related reading
- Sum of number of divisor of number between a and b inclusive
- Support Resistance Algorithm - Technical analysis
- SURF vs SIFT, is SURF really faster?
- SVM and Neural Network
- Support Vector Machine library for C
- swagger .net core API ambiguous HTTP method for Action Error
- Swap two integers without using a third variable
- Swift - Sort array of objects with multiple criteria

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.