• ### Popular Now

• 12
• 12
• 9
• 10
• 13

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok i know what modulus does, but i don't know how to use it to my advange so here is my question #include <iostream> using namespace std; int main() { unsigned short small; unsigned long large; unsigned long skip; unsigned long target; const unsigned short MAXSMALL=65535; cout << "Enter a small number: "; cin >> small; cout << "Enter a large number: "; cin >> large; cout << "Enter a skip number: "; cin >> skip; cout << "Enter a target number: "; cin >> target; cout << endl; while (small < large && large > 0 && small < MAXSMALL) { small++; // incremeting small if (small % skip == 0) // my question is what does this do? { cout << "skipping on " << small << endl; continue; } if (large == target) { cout << "Target reached!\n"; break; } large -= 2; } cout << "Small : " << small << " Large: " << large << endl; system("PAUSE"); return 0; }

##### Share on other sites
Modulus calculates the remainder of a division.

Examples:
4 % 3 = 1. (4 / 3 = 1, 1 left over)
4 % 2 = 0. (4 / 2 = 2, 0 left over)
4 % 1 = 0. (4 / 1 = 4, 0 left over)

if (small % skip == 0) // my question is what does this do?{     cout << "skipping on " << small << endl;     continue;}

Think of it in terms of actual numbers.
if (4 % 4 == 0) // my question is what does this do?{     cout << "skipping on " << small << endl;     continue;}

So it'll skip the #4, because 4 % 4 = 0 (4 / 4 = 1, 0 left over).

##### Share on other sites
Quote:
 Original post by Ekim_GramYou posted the code but we need a more detailed question about what your asking.

ok, im wondering on the line
if (small % skip == 0) // my question is what does this do?
{
cout << "skipping on " << small << endl;
continue;
}
what does (small % skip == 0); do here in detail and why does the % operator does in full detail?

oh dang, ok i see now
thank you! :)

##### Share on other sites
Oops, didn't see the comment in your code. That's basically saying if there is no remainder when you divide the two numbers, it performs the instructions given to it.

##### Share on other sites
Quote:
 Think of it in terms of actual numbers.

This is good advice. For example if you want to know what numbers are selected for (a % b == 0), then it will be those a equal to:

0, b, 2*b, 3*b, 4*b, ...

Similarly if you have (a % b == 1), you get:

1, b+1, (2*b)+1, (3*b)+1, ...

HTH,
Jim.