0
Write a program to sum of digits of given integer number
5 ответов
+ 3
Could you please add a tag which language is meant? I've seen in your profile that you are learning java, cpp and python but no codes.
+ 2
You can generally add up the digits of a number by repeatedly adding the remainder of the number and 10 to some variable 'sum' and afterwards dividing the number by 10, until the number becomes 0.
+ 2
Shadow Well not my question but interesting solution. However it only works for numbers < 100!
To make an example 53:
53 div 10 = 5
remainder 3 + 10 = 30 div 10 = 3
remainder 0
But 124:
124 div 10 = 12
remainder 4 ...
but 12 is no digit
+ 2
Mirko Klotzsche This is how it should run for 124:
sum = 0;
num = 124;
sum += num % 10 // sum + 4
num /= 10 = 12
sum += num % 10 // sum + 2
num /= 10 = 1
sum += num % 10 // sum + 1
num /= 10 = 0
num is 0, loop stops.
sum is 7 now.
I don't see why this shouldn't work? 🤔
+ 2
Oh I see ... backwards!