Recursive Sum of Digits
In financial technology, data integrity and quick computational checks are crucial. Imagine a scenario where you're validating transaction IDs or account numbers where a simple checksum is derived by summing its digits. This problem provides a foundational exercise in applying recursion to solve such a task efficiently, a pattern often seen in data processing and validation pipelines.
Implement a function sumDigitsRecursive that takes a non-negative integer n and returns the sum of its digits using a recursive approach.
n: number
number
n.n = 0? The sum of digits for 0 is 0.n is a single-digit number? The sum of digits is the number itself (e.g., sum of digits for 5 is 5).Examples
Example 1
Input:
n = 123
Output:
6
Note:
1 + 2 + 3 = 6
Example 2
Input:
n = 4509
Output:
18
Note:
4 + 5 + 0 + 9 = 18
Example 3
Input:
n = 7
Output:
7
Note:
The sum of digits for a single-digit number is the number itself.
Example 4
Input:
n = 0
Output:
0
Note:
The sum of digits for 0 is 0.
Constraints
0 <= n <= 10^9n will always be an integer.n = 0 and single-digit numbers.Loading editor...