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.
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.
>> is this right
Why don't you just try it and see.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
i agree with Hammer. but you do not need a special libary for %. you do need cmath though if you are planning to % doubles.
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 } }
"You can lead a man to Congress, but you can't make him think."
"The Grand Old Duke of York
-He had ten thousand men.
-His case comes up next week."
"Roses are red, violets are blue, I'm schizophrenic, and so am I."
"A computer once beat me at chess, but it was no match for me at kick boxing."
"More and more of our imports are coming from overseas."
--George W. Bush
"If it weren't for electricity, we'd all be wacthing TV by candlelight."
--George W. Bush
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?
"You can lead a man to Congress, but you can't make him think."
"The Grand Old Duke of York
-He had ten thousand men.
-His case comes up next week."
"Roses are red, violets are blue, I'm schizophrenic, and so am I."
"A computer once beat me at chess, but it was no match for me at kick boxing."
"More and more of our imports are coming from overseas."
--George W. Bush
"If it weren't for electricity, we'd all be wacthing TV by candlelight."
--George W. Bush
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".
also, in your if statements, it needs to be:
notCode:if(b == 0)Code:if(b = 0)
oh duh, thx alpha, my stupidity never ceases to amaze me
"You can lead a man to Congress, but you can't make him think."
"The Grand Old Duke of York
-He had ten thousand men.
-His case comes up next week."
"Roses are red, violets are blue, I'm schizophrenic, and so am I."
"A computer once beat me at chess, but it was no match for me at kick boxing."
"More and more of our imports are coming from overseas."
--George W. Bush
"If it weren't for electricity, we'd all be wacthing TV by candlelight."
--George W. Bush
no problem, I've done it too. Accidental typos here and there. Happens to everyone.
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
"You can lead a man to Congress, but you can't make him think."
"The Grand Old Duke of York
-He had ten thousand men.
-His case comes up next week."
"Roses are red, violets are blue, I'm schizophrenic, and so am I."
"A computer once beat me at chess, but it was no match for me at kick boxing."
"More and more of our imports are coming from overseas."
--George W. Bush
"If it weren't for electricity, we'd all be wacthing TV by candlelight."
--George W. Bush
try
Code:if(b = n % i) { } else { }
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
than wouldnt it be pointless to haveCode:b = n % i
thats like saying int i = 2, if i == 2..... you get the pointCode:if(b == n % i) { }
however, i do appreciate your trying to help me, and what you said probably works and i just misunderstood it.
Last edited by Geo-Fry; 03-28-2003 at 01:52 PM.
"You can lead a man to Congress, but you can't make him think."
"The Grand Old Duke of York
-He had ten thousand men.
-His case comes up next week."
"Roses are red, violets are blue, I'm schizophrenic, and so am I."
"A computer once beat me at chess, but it was no match for me at kick boxing."
"More and more of our imports are coming from overseas."
--George W. Bush
"If it weren't for electricity, we'd all be wacthing TV by candlelight."
--George W. Bush
nm i read your code wrong, my mistake.
Use of else:
Code:if(x) { // do this } else { // Only do this if not x }THIS WORKS FOR ME: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... }
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