Sum of Digits in an Integer

(Before you yell as best as you can through the 'net, yes, I CAN use the "Search" button.)

I've found this page:
http://www.cplusplus.com/forum/beginner/4988/
to be quite helpful. It explains it very well. You find the last digit, divide by ten, repeat. But I have a problem.

For example, if your number was 179, you start off with a modulus. You get 9. Then divide by 10, you get 17.9. Why does the 9 get "chopped off"? And why doesn't it round up to 18?
Whelp?
As you noticed, 9 indeed does get "chopped off". That's how integer division works. No rounding. It's exactly the result a little kid would give you if you asked it "what is 179/10?". It would tell you: "it's 17 and the remainder is 9".
Last edited on
Integer division works that way.

3 goes into 8 only 2 times. Hence, 8 / 3 == 2.

Hope this helps.
Thanks, solved! Both great explanations. =)
Topic archived. No new replies allowed.