14th Jan 2021, 6:04 AM
Ira Sarkar
Ira Sarkar - avatar
5 ответов
+ 6
The modulo operation returns the remainder, but it does not remove the digit from the original number. Here is an example of what is happening: n = 11111 a = n%10000; // a = 1 sum =+ a; // sum = 1 a = n%1000; // a = 11 sum = sum + a; // sum = 12 a = n%100; // a = 111 sum = sum + a; // sum = 123 a = n%10; // a = 1111 sum = sum + a; // sum = 1234 sum = sum + n; // sum = 12345 What you need to do is include an operation to remove a digit as you sum each one. To do this, divide the number by 10 (n/10) after each summation. You also should start with n%10 instead so you are working with the right most digit, and continue to do so until all digits are summed. Similar to this: a = n%10; sum =+ a; n = n/10; a = n%10; sum = sum + a; a = n/10; ... repeat until all digits are processed.
14th Jan 2021, 6:32 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 2
Ira Sarkar You’re very close! Just one correction is needed on line 17. It should be n%10, not n%100. Fix that and retest with some simple numbers like 11111 and 10000 to confirm.
18th Jan 2021, 2:35 PM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 1
Elizabeth Kelly thanks 😅
14th Jan 2021, 8:03 AM
Ira Sarkar
Ira Sarkar - avatar
+ 1
Elizabeth Kelly minor mistake 😅😅 Thanks a lot!
18th Jan 2021, 2:38 PM
Ira Sarkar
Ira Sarkar - avatar
0
Elizabeth Kelly can you please look again? 😅
18th Jan 2021, 2:10 PM
Ira Sarkar
Ira Sarkar - avatar