Find the Sum of the Digits of a Number
Compute the sum of all digits of an integer using arithmetic only.
Peel off the last digit with number % 10, add it to a running total, then drop that digit with number / 10. Repeat until the number becomes 0. Work with the absolute value so negative inputs sum the same digits as their positive counterparts.
Problem Statement
Given an integer, return the sum of its digits. For example, the digits of
1234 are 1, 2, 3, 4, and their sum is 10.
Treat negative numbers by summing the digits of their absolute value, so
-99 gives 18. Do not convert the number to a String.
Input: A single integer n.
Output: An integer equal to the sum of the digits of n.
Examples
Input: 1234
Output: 101 + 2 + 3 + 4 = 10.
Input: -99
Output: 18The sign is ignored; 9 + 9 = 18.
Constraints
-2,147,483,648 <= n <= 2,147,483,647No String conversion
Think Before You Code
Reveal the questions to ask yourself first
- How do you read the last digit of a number without a String? (
% 10) - How do you remove that digit once you have added it? (
/ 10) - When should the loop stop?
- How should a negative number be treated so its digits sum correctly?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Walk the number one digit at a time from the least significant end.
- Convert
nto its absolute value so the sign does not interfere. - Start
sumat 0. - While the value is not 0:
digit = value % 10— the current last digit.sum += digit— accumulate it.value = value / 10— drop the digit you just used.
- Return
sum.
Dry Run
Walk through the example step by step
Summing the digits of n = 1234:
step | value | digit = value % 10 | sum
-----+-------+--------------------+----
1 | 1234 | 4 | 4
2 | 123 | 3 | 7
3 | 12 | 2 | 9
4 | 1 | 1 | 10
end | 0 | — | 10
Solution
Reveal the full Java solution
public class SumOfDigits {
public static int sumOfDigits(int n) {
long value = Math.abs((long) n); // long guards against Integer.MIN_VALUE
int sum = 0;
while (value != 0) {
int digit = (int) (value % 10);
sum += digit;
value /= 10;
}
return sum;
}
public static void main(String[] args) {
System.out.println(sumOfDigits(1234)); // 10
System.out.println(sumOfDigits(-99)); // 18
}
}
The % 10 and / 10 pair is the workhorse of every digit problem: modulo
exposes the last digit, integer division discards it. Casting to long before
Math.abs matters because the absolute value of Integer.MIN_VALUE does not
fit in a positive int, and the loop terminates naturally once repeated
division reaches 0.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Looping while (n > 0), which skips negative numbers entirely.
- Using Math.abs on the int directly, overflowing for Integer.MIN_VALUE.
- Resetting the sum inside the loop instead of accumulating it.
Edge Cases to Test
- n = 0 returns 0 (the loop body never runs).
- Single-digit numbers return themselves.
- Integer.MIN_VALUE, whose magnitude needs a long.
Interview Follow-Ups
- How would you keep summing until the result is a single digit (the digital root)?
- Can you compute the digit sum recursively instead of with a loop?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →