Loading...
Loading...
Loading...
Kavya has a lucky-number game with her hostel friends. Each friend picks a number, and the score is the sum of its digits. For 4096 the score is 4 + 0 + 9 + 6 = 19.
Given the number n, print its digit sum. Write the digit sum as a method that calls itself.
Example 1
Input
4096
Output
19
4 + 0 + 9 + 6 = 19.
Example 2
Input
7
Output
7
A single digit is its own sum.
Try the problem first. Open one hint at a time.
The last digit of n is n % 10, and n / 10 is n without its last digit.
The digit sum of n is its last digit plus the digit sum of n / 10.
Stop when n becomes 0: the digit sum of 0 is 0.
n can be up to 10^18, so keep it in a long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor