Sum of digits

Problem #11

Tags: arithmetic modulo

Who solved this?

Also available in: Spanish Russian

This programming exercise is intended to introduce numeral system basics to you. We start learninig this concept by playing with decimal system which we use everyday (though you should keep in mind that computer does not use it internally - it only converts numbers to it when they should be shown to user).

As any number greater than 9 is represented by several digits, we can calculate the sum of these digits. For example, for numbers 1492 and 1776 we get:

1 + 4 + 9 + 2 = 16
1 + 7 + 7 + 6 = 21

In this task you will be given several numbers and asked to calculate their sums of digits.

Important: while many programming languages have built-in functions to convert numbers to strings (from which digits could be extracted), you should not use this (since your goal is to learn some programming tricks).

Instead you need to implement algorithm with repetitive division by 10 (base of numeral system) and summing up the remainders. Read the Number to digits article for details on the algorithm.

Problem statement

Input data are in the following format:

Answer should have N results, also separated by spaces. For example:

input data:
3
11 9 1
14 90 232
111 15 111

answer:
1 16 21

Here the first case requires to calculate 11*9+1 = 100 and its sum of digits is 1+0+0 = 1.

You need to login to get test data and submit solution.