Practice

Recursive Sum of Digits

2024/Oct/Nov·Variant 1
MEDIUMRecursion

Write a recursive function SumDigits(N) that returns the sum of the decimal digits of a non-negative integer N.

Input: One non-negative integer. Output: The sum of its digits.

Example:

Input:  48291
Output: 24
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Single digit — base case only
Inputs: 5
Expected: 5
Test 2: Five-digit number
Inputs: 48291
Expected: 24
Test 3: Zero
Inputs: 0
Expected: 0