0
Anyone help me please.
5 Réponses
+ 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.
+ 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.
+ 1
Elizabeth Kelly thanks 😅
+ 1
Elizabeth Kelly minor mistake 😅😅
Thanks a lot!
0
Elizabeth Kelly can you please look again? 😅