tags:

views:

120

answers:

3

You need to use division and remainder by 10. Consider this example:

  • 163 divided by 10 is 16, remainder 3
  • 16 divided by 10 is 1, remainder 6
  • 1 divided by 10 is 0, remainder 1

You'll notice the remainder is always the last digit of the number that's being divided. How do I do this in C?

+1  A: 

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#)

moogs
A: 

With the modulus operator (%):

15 % 12 == 3
17 % 8 == 1
Ignacio Vazquez-Abrams
ok how would i then make the results an individual variable for futher calculations?
Simply assign the result of the modulus operation to another int variable, the same as you would with `+` or `*`.
Ignacio Vazquez-Abrams
+4  A: 

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.

Mark Byers