Java Program for Sum of Digits Using Recursion
int sumOfDigits(int n) { if (n == 0) return 0; return n % 10 + sumOfDigits(n / 10); } which adds the last digit to the sum of the remaining digits recursively.Examples
How to Think About It
n % 10 to get the last digit and n / 10 to remove it. Keep adding the last digit to the sum of the remaining digits until the number becomes zero.Algorithm
Code
public class SumOfDigits { public static int sumOfDigits(int n) { if (n == 0) { return 0; } return n % 10 + sumOfDigits(n / 10); } public static void main(String[] args) { int number = 123; System.out.println("Sum of digits of " + number + " is " + sumOfDigits(number)); } }
Dry Run
Let's trace the number 123 through the recursive sumOfDigits method.
Initial call
sumOfDigits(123) calls sumOfDigits(12) and adds 3 (123 % 10)
Second call
sumOfDigits(12) calls sumOfDigits(1) and adds 2 (12 % 10)
Third call
sumOfDigits(1) calls sumOfDigits(0) and adds 1 (1 % 10)
Base case
sumOfDigits(0) returns 0
Return values
sumOfDigits(1) returns 1 + 0 = 1 sumOfDigits(12) returns 2 + 1 = 3 sumOfDigits(123) returns 3 + 3 = 6
| Call | n | n % 10 | Recursive call with n/10 | Return value |
|---|---|---|---|---|
| sumOfDigits(123) | 123 | 3 | sumOfDigits(12) | 6 |
| sumOfDigits(12) | 12 | 2 | sumOfDigits(1) | 3 |
| sumOfDigits(1) | 1 | 1 | sumOfDigits(0) | 1 |
| sumOfDigits(0) | 0 | 0 | base case | 0 |
Why This Works
Step 1: Base case stops recursion
When the number becomes zero, the function returns 0 to stop further recursive calls.
Step 2: Extract last digit
Using n % 10 extracts the last digit of the current number.
Step 3: Recursive call reduces problem size
Calling the function with n / 10 removes the last digit, making the problem smaller each time.
Step 4: Sum builds up on return
Each recursive call returns the sum of digits found so far, adding the last digit at each step.
Alternative Approaches
public class SumOfDigits { public static int sumOfDigits(int n) { int sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } public static void main(String[] args) { int number = 123; System.out.println("Sum of digits of " + number + " is " + sumOfDigits(number)); } }
public class SumOfDigits { public static int sumOfDigits(int n) { String s = Integer.toString(n); int sum = 0; for (char c : s.toCharArray()) { sum += c - '0'; } return sum; } public static void main(String[] args) { int number = 123; System.out.println("Sum of digits of " + number + " is " + sumOfDigits(number)); } }
Complexity: O(d) time, O(d) space
Time Complexity
The function calls itself once per digit, so time grows linearly with the number of digits d.
Space Complexity
Each recursive call adds a layer to the call stack, so space also grows linearly with d.
Which Approach is Fastest?
The iterative method uses constant space and is faster in practice, but recursion is elegant and easy to understand.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive | O(d) | O(d) | Learning recursion and elegant code |
| Iterative | O(d) | O(1) | Performance and memory efficiency |
| String conversion | O(d) | O(d) | Simple code but less efficient |