# Digit_sum

#1

My code works fine, but my general doubt is how does the modulus works?
For example:
If I take digit_sum(432)
After 2 iterations - sum = 5 and n = 4
so in the next iteration why is 4%10 is = 4
I expected it to be 0 or some absurd value like 6(10-4 = 6)

``````def digit_sum(n):
sum = 0
while n>0:
r= n%10
sum = sum +r
n=n/10
return sum``````

#2

About the operator, the modulo (%) gives the remainder from the division of the first argument by the second.

#3

yes i know that ,
but how does modulus work when the numerator is lesser than the denominator?
and can you give some examples what modulus produce in case of negative numbers?

#4

Oops! Sorry about that. I really struggled to get your question. Find attached!

#5

OK, so finally a remainder should always be the SMALLEST WHOLE number that we can get by adding or subtracting (Numerator) from (Denominator x Quotient) where a quotient can range from negative to positive integers.
The above sentence may be a bit confusing as i may have not framed it properly.
But, I got my concept cleared.
Thanks a lot

#6

You're welcome.

Have fun coding!

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.