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)
n
Digits
Sum
12345
1+2+3+4+5
15
-802
8+0+2
10
0
0
0
7
7
7
Approach
How to Find the Sum of Digits in C#
Four clear steps — modulo and integer division do all the work.
Take Math.Abs(n) so negatives share the same path.
While n != 0, add n % 10 to a total.
Remove the last digit with n /= 10.
When n becomes 0, return the total.
Method
Idea
Time
Best for
Modulo loop
% 10 then / 10
O(digits)
Interviews, default answer
String digits
Convert to string and sum chars
O(digits)
Quick hacks (less preferred)
Repeated sum
Keep summing until one digit
O(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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Last digit
n % 10
Drop digit
n /= 10
Stop when
n == 0
Negatives
Math.Abs(n) first
Zero
Digit sum is 0
vs Condense
One pass only (not digital root)
Classic check
12345 → 15
Try it
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
Trace
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
Code
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#
using System;
class Program
{
static int DigitSum(int number)
{
number = Math.Abs(number);
int total = 0;
while (number != 0)
{
total += number % 10;
number /= 10;
}
return total;
}
static void Main()
{
int number = 12345;
Console.WriteLine("The sum of digits of " + number + " is: " + DigitSum(number));
}
}
Output
The sum of digits of 12345 is: 15
How It Works
Math.Abs keeps the helper safe for negatives.
Each loop takes the last digit with number % 10.
number /= 10 drops that digit for the next pass.
For 12345 the digits added are 5, 4, 3, 2, then 1.
When number hits 0, return 15.
Example 2 — Sum of Digits (User Input)
Read one integer and reuse the same helper.
C#
using System;
class Program
{
static int DigitSum(int number)
{
number = Math.Abs(number);
int total = 0;
while (number != 0)
{
total += number % 10;
number /= 10;
}
return total;
}
static void Main()
{
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine("Sum of digits: " + DigitSum(number));
}
}
Output
Enter a number: 12345
Sum of digits: 15
How It Works
Parse one integer from the console.
Pass it to DigitSum — input and logic stay separate.
Negatives work because the helper uses Math.Abs.
Try -802 to confirm the answer is 10.
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#
using System;
class Program
{
static void Main()
{
int n = -802;
int x = Math.Abs(n);
int total = 0;
Console.WriteLine("Starting from Math.Abs(" + n + ") = " + x);
while (x != 0)
{
int digit = x % 10;
total += digit;
Console.WriteLine(" take " + digit + ", remaining " + (x / 10)
+ ", total=" + total);
x /= 10;
}
Console.WriteLine("Digit sum: " + total);
}
}
Output
Starting from Math.Abs(-802) = 802
take 2, remaining 80, total=2
take 0, remaining 8, total=2
take 8, remaining 0, total=10
Digit sum: 10
How It Works
Absolute value turns -802 into 802 before peeling.
Digits arrive as 2, then 0, then 8.
A zero digit still counts as a step but adds nothing.
When remaining is 0, the loop stops.
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 Math.Abs
C# % keeps the dividend’s sign. Normalize 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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Modulo digit loop
O(digits) = O(log n)
O(1)
User-input + digit sum
O(digits)
O(1)
Trace peel
O(digits)
O(1)
Each digit is visited once. The number of digits grows like log₁₀ n.
Remember
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 Math.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 Math.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.