What is wrong with this program

I am working on this exercise from a C++ book called PrimeNumbers. 1) create a function that determines whether a number is prime 2. Use the function in a program which determines and prints all the prime numbers from 2-10,000 

Here is the code:

<code> #include <iostream>

using std::cout;
using std::endl;

void primeNumber(int);

 

int main() {
cout<<"Prime Numbers"<<endl;
for (int i=2;i<=10000;i++) {
primeNumber(i);
}


}

void primeNumber(int number) {
int total=0;
for (int i=2;i<number;i++) {
if (number%i==0) {
total++;
} else {
continue;
}


}
if (total==0){
cout<<number<<endl;

}
}

</ code>

 

I think I'm doing everything right but everytime I try to run the program it only displays the prime numbers starting from 7331. I have no idea why it is doing this. 

 

 

I'm assuming this is C++, correct?

It compiles perfectly fine for me.

So it displays the prime numbers by starting from 2?

You are writing your C++ rather verbosely, and you don't have to.

For example, this does the exact same thing, but with quite a few less lines.

It compiles perfectly fine, for me, using G++ 4.6.2.

I don't see anything different from your code than mine except "using namespace std;". I know thats the more common way to that, but im not too far into the book so it starts of by making you write the statement in different lines. I don't know, i just thought doing that might help me understand later what is going on when they introduce that. And also what could be wrong with my compiler? 

Same code, better formatting. You don't need brackets for one line statements.

Ajit, there is nothing wrong with your code.

The confusion point is that you are printing prime numbers to a command prompt. Command prompt is limited in terms of number of lines it can display at the same time. So your program cannot fit all prime numbers. That is why it only displays last prime numbers between 7331 and 10000.

With following code you should be able to see all prime nnumbers printed in command prompt:

if (total == 0){

               cout << number << ", ";  // no new line character

}

Looks good but why not terminate the loop straight away rather than counting the number of factors? Just an item of curiosity as it seems you don't use 'total' in your code other than to check whether it's zero, so once it's been incremented once you don't need to put in the work to see if you need to increment it further.

You could just print the prime numbers to a text file. That way you dont have to deal with a jumbled mess of numbers or have the numbers get cut off in the console. You can also do what Aurenkin said. cuts way down on computation time. you could also check only the odd numbers as there are no even prime numbers after 2.

out of curiosity why are you creating your function declaration thingy (not in touch with the c++ lingo)

"void primeNumber(int);"

above the actual function? Isn't that only useful when you want to have functions appear after main and you want main to reference those functions?

why do they even teach it that way still? when we transitioned from C to C++ in HS they gave a brief intro to that and why it wastes time/space and that was that. :-p

Also, my coding ADD says put the Main first. :-D