0

Write a program to sum of digits of given integer number

https://www.sololearn.com/discuss/1316935/?ref=app

1st Nov 2018, 6:42 PM
Khaled Shabana
Khaled Shabana - avatar
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.
1st Nov 2018, 6:56 PM
Mirko Klotzsche
Mirko Klotzsche - avatar
+ 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.
1st Nov 2018, 6:59 PM
Shadow
Shadow - avatar
+ 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
1st Nov 2018, 7:24 PM
Mirko Klotzsche
Mirko Klotzsche - avatar
+ 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? 🤔
1st Nov 2018, 8:20 PM
Shadow
Shadow - avatar
+ 2
Oh I see ... backwards!
1st Nov 2018, 8:24 PM
Mirko Klotzsche
Mirko Klotzsche - avatar