Sum of Digits
Practice this coding question on Coding Gurukul.
Problem statement
A school coding club creates a numeric badge activity. Each badge has a decimal number, and its activity score is the sum of all digits printed on it. The same digit can appear more than once and each occurrence contributes to the score. The organiser supplies several badge numbers and wants a separate score for each one. Your task Read each nonnegative integer n and add its decimal digits. Print the resulting digit sum. Process all test cases independently and keep their original order. You may inspect the digits as characters or extract them arithmetically. Important details Zero has digit sum zero. A zero inside a larger number adds nothing but does not end the calculation. Do not keep summing the result until one digit remains; only one pass over the original digits is required. Worked example Badge 5074 has score 5 + 0 + 7 + 4 = 16. The answer is 16, not the further-reduced value 7.
Input format
First line: T. Next T lines: n.
Output format
Print one digit sum per test case.
Constraints
1 <= T <= 1000; 0 <= n <= 1000000000.
Sample input
2 5074 99
Sample output
16 18
Sign in to run or submit a solution in C++, Java or Python.