# Thread: C++ math operators (the remainder one)

1. ## C++ math operators (the remainder one)

hey can someone tell me how to use the operator that tells you the remainder whn you divided a number, i think its % and you do like 15%4 and that would give you 3. is this right? also which library do i need for it? thanks in advance.

2. >> is this right
Why don't you just try it and see.

3. i agree with Hammer. but you do not need a special libary for %. you do need cmath though if you are planning to % doubles.

4. ok i tried it and i think it worked... there was no error or anything, but it didnt give the result i wanted. i am making a program to test for primeness (is that a word?) in a number. here is what i have, but it just says every number is prime:

also in my for loop i am changing the middle part (forgot what it was called) to be the square root of N instead of half of N, but bare with for now.

Code:
```#include <iostream>

int main()
{
int i = 2; //the first number it tests
int n;
int b;
while(1)
{
cout <<"Enter a number to test.\n";
cin >>n;
b = (n % i); //be is the remainder of their number and 2
if(b = 0)    //if there is no remainder...
{
cout <<n <<" is not prime. It is divisible by 2."; //...its not prime
}
for(i = 3; i<(n / 2); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
{
b = (n % i); //same as before, except instead of 2 its the next odd value
if(b = 0)   //if no remainder...
{
cout <<n <<" is not prime. It is divisible by " << i <<".\n\n";  //...then its not prime
}
}
cout <<n <<" is prime.\n\n"; //once i > .5n (at which point its redundent) it declares it prime
}
}```

5. ok thanks rod the % makes a lot more sense now, but i dont understand what i did wrong in my program... why does it aways come up as prime, even if you put in 4 or 12?

6. ## Looks like a good attempt... A couple of things I noticed:

You need some "else" statements to go with those "if" statements. Or maybe "break;"

Your code will print the "is prime" message whether it prints "not prime" or not.

Also, the loop keeps counting up after it discovers "not prime".

7. also, in your if statements, it needs to be:

Code:
`if(b == 0)`
not
Code:
`if(b = 0)`

8. oh duh, thx alpha, my stupidity never ceases to amaze me

9. no problem, I've done it too. Accidental typos here and there. Happens to everyone.

10. ive put in all the corrections people have made (or so i believe), yet still every number is a prime. here is my latest attemp:

Code:
```#include <iostream>
#include <cmath>

int main()
{
int i = 2;                           //the first number it tests
int n;
int b;
while(1)
{
cout <<"Enter a number to test.\n";
cin >>n;
b = (n % i);                       //be is the remainder of their number and 2
if(b == 0)                         //if there is no remainder...
{
cout <<n <<" is not prime. It is divisible by 2."; //...its not prime
}
else
{
for(i = 3; i < sqrt(n); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
{
b = (n % i);                     //same as before, except instead of 2 its the next odd value
cout <<i <<"\n";            //show the last attempt, no reason, just for fun i guess
if(b == 0)                       //if no remainder...
{
cout <<n <<" is not prime. It is divisible by " << i <<".\n\n";  //...then its not prime
}   //end if
else                             //if its not yet known, it just repeats the loop
{
}   //end else
}     //end for
cout <<n <<" is prime.\n\n";       //once i > .5n (at which point its redundent) it declares it prime
}       //end else
}         //end while
}           //end main```

11. try

Code:
```if(b = n % i)
{
}
else
{
}```

12. i tried your suggestion rod, and it didnt seem to make any difference, and i didnt really understand what you were getting at when you said that... if i say
Code:
`b = n % i`
than wouldnt it be pointless to have
Code:
```if(b == n % i)
{
}```
thats like saying int i = 2, if i == 2..... you get the point
however, i do appreciate your trying to help me, and what you said probably works and i just misunderstood it.

14. Use of else:
Code:
```if(x)
{
// do this
}
else
{
// Only do this if not x
}```
Code:
```if(x)
{
// do this only if x is not zero (x=true)
}
else
{
// Only do this only if x is zero (x=false)
}

{
// ALWAYS do this...

}```
THIS WORKS FOR ME:
Code:
```#include <iostream>
#include <cmath>
using namespace std;				// *** ADDED BY DOUG (reqd for MSVC++)

int main()
{
int i = 2;                           //the first number it tests
int n;
int b;
while(1)
{
cout <<"Enter a number to test.\n";
cin >>n;
b = (n % i);                       //be is the remainder of their number and 2
if(b == 0)                         //if there is no remainder...
{
cout <<n <<" is not prime. It is divisible by 2."; //...its not prime
return 0;							// *** ADDED BY DOUG
}

else
{
for(i = 3; i < sqrt(n); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
{
b = (n % i);                     //same as before, except instead of 2 its the next odd value
cout <<i <<"\n";            //show the last attempt, no reason, just for fun i guess
if(b == 0)                       //if no remainder...
{
cout <<n <<" is not prime. It is divisible by " << i <<".\n\n";  // ***MOVED BY DOUG
return 0;						// *** ADDEDE BY DOUG
}   //end if
else                             //if its not yet known, it just repeats the loop
{
cout <<n <<" is prime.\n\n";       //once i > .5n (at which point its redundent) it declares it prime
return 0;						// *** ADDED BY DOUG
}   //end else
}     //end for

}       //end else
}         //end while
return 0;								// *** ADDED BY DOUG
}           //end main```