|
[28 Jan 2002|09:19pm] |
| [ |
mood |
| |
confused |
] |
Hi, remember that I said I was taking C++ in school? Well, I'm having a problem with a couple of programs, and wondering if I could get some help on one of them here.
We're supposed to write a bool function that returns true if a number is prime and false if it isn't, then write a loop in main() that calls it for each number between 2 and 1000, and counts all the numbers that are prime. The loop in main is easy enough, but I'm having some problems with the function. Here's what I have:
bool isItPrime(int num) { int i; bool isprime; for (i=2; i [ Error: Irreparable invalid markup ('<num;>') in entry. Owner must fix manually. Raw contents below.] Hi, remember that I said I was taking C++ in school? Well, I'm having a problem with a couple of programs, and wondering if I could get some help on one of them here.
We're supposed to write a bool function that returns true if a number is prime and false if it isn't, then write a loop in main() that calls it for each number between 2 and 1000, and counts all the numbers that are prime. The loop in main is easy enough, but I'm having some problems with the function. Here's what I have:
<code>bool isItPrime(int num) { int i; bool isprime; for (i=2; i<num; i++) { if (num%i==0) {isprime=false} } return isprime; }</code>
At least that's what I remember I had. I'll verify after tomorrow if necessary. This (or whatever I had) counts there as being 169 prime numbers between 2 and 1000, when there's only 168. A friend and I are both stumped.
Any help would be appreciated. :)
|
|