Loading...
Loading...
Loading...
A payments app runs a quick check on each UPI transaction ID: it adds up the digits. Print the digit sum of the given ID.
Write a method static int digitSum(long n) that returns the sum of the digits of n.
Example 1
Input
4072
Output
13
4 + 0 + 7 + 2 = 13.
Example 2
Input
0
Output
0
The only digit is 0.
Try the problem first. Open one hint at a time.
n % 10 gives the last digit and n / 10 removes it.
Repeat while n > 0, adding each last digit to a total.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor