Sum of Digits
Practice this coding question on Coding Gurukul.
Problem statement
A parcel scanner creates a simple audit total by adding the decimal digits of a parcel number. Each occurrence of a digit contributes separately. Your task Given a nonnegative integer n, compute the sum of all its decimal digits. Important details Zeros contribute nothing but are still part of the written number. Do not repeatedly sum the result until a single digit remains; only one pass over the original digits is requested. The answer for n = 0 is zero. Extract digits with integer arithmetic or inspect their decimal representation, taking care to add numerical digit values rather than character codes. Worked example Parcel number 5087 contributes 5 + 0 + 8 + 7 = 20. The answer is 20, not the digit sum 2 of that intermediate result.
Input format
One integer n.
Output format
Print its digit sum.
Constraints
0 <= n <= 1000000000.
Sample input
5087
Sample output
20
Sign in to run or submit a solution in C++, Java or Python.