开发者

Finding the Remainder after Division in C

开发者 https://www.devze.com 2022-12-25 01:38 出处:网络
You need to use division and remainder by 10, Consider this example,开发者_JS百科 163 divided by 10 is 16 and remainder is 3

You need to use division and remainder by 10,

Consider this example,开发者_JS百科

163 divided by 10 is 16 and remainder is 3
16 divided by 10 is 1 and remainder is 6
1 divided by 10 is 0 and remainder is 1

Notice that the remainder is always the last digit of the number that's being divided.

How can I do this in C?


It looks like homework so I won't give you code, but I suggest you research the modulo operator and how this could be used to solve your assignment.


Use the Modulus operator:

 remainder = 163 % 10; // remainder is 3

It works for any number too:

  remainder = 17 % 8;  // remainder is 1, since 8*2=16

(This works for both C and C#)


With the modulus operator (%):

15 % 12 == 3
17 % 8 == 1
0

精彩评论

暂无评论...
验证码 换一张
取 消