Please Help | Sololearn: Learn to code for FREE!

+1

Please Help

I want to calculate the sum of all digits of the input number. For example Input= 643 Output= 13 The sum of the digits of 643 is 6+4+3 = 13. I tried to do it but it doesn’t work, can someone help me, Thanks https://code.sololearn.com/cpJ4JWaVLTbb/?ref=app

10/16/2020 12:45:22 AM

Alejandra b

3 Answers

New Answer

+3

Quick answer : digit = n % 10 num += digit n = n // 10 Explanation : With your current version n %= 10 You keep updating n as the remainder For example, 123 n will always be 3 infinite loop So you should update n by true division of n, For example, 123 -> 12 12 -> 1 And for the digit, use another variable to modulus %= 10

+3

https://code.sololearn.com/c0giYx7bLTjC/?ref=app

+1

If u are using python there is a general answer which Gordon has already mentioned if u don't like to do the way he said just convert number to string and acess digits by index and then convert to number n=str(n) sum=int(n[0]) + int(n[1])+ int(n[2])