### how do you use mod

how do you find the ones tens and hundreds place in a single int
The modulus % returns the remainder of integer division. Say you have an integer 1,234 - what number would you have to divide 1,234 by to get a remainder of 4 (the ones place)? You'd use that number with the modulus operator to get the ones place.
Let X / Y = Q + M / Y

where Q is the quotient and M is the numerator of the remainder. The modulus operator returns M.

Therefore:
X - Q*Y = M
Or, in programming:
X % Y = M

Therefore, to find the ten's place of a number, let Y = 10 and N be the number we are given:
X = N / 10 (Divide by ten first, so the ten's place becomes the one's place)
M = X % 10
M = X - Q*10

Now let N be for example 1234:
X = 1234 / 10 = 123
M = 123 % 10
M = 123 - 12*10 (Q = 12 because integer division has no decimal portion)
M = 123 - 120 = 3

Therefore, the ten's place digit of 1234 is 3.
 ``12345678910111213141516171819202122232425`` ``````#include int main(){ using namespace std; int N = 1234, Y = 10, X = N/Y, Q = X/Y, //Becomes 12 M1 = X%Y, // Using the modulus operator M2 = X-Q*Y //Using the longer method ; cout << "N: " << N << '\n' << "X: " << X << '\n' << "Y: " << Y << '\n' << "Q: " << Q << '\n' << "M1: " << M1 << '\n' << "M2: " << M2 << '\n' ; return 0; }``````

http://ideone.com/GwpvoW

The key operations here are dividing and modulating. That is how you can separate an integer into its individual digits.
thanks
Topic archived. No new replies allowed.