# Thread: Prime number program help?

1. ## Prime number program help?

Hey I was wonderin if someone could help me with my program I am having trouble on how to get it to work properly...so this is what I have so far

Code:
```#include <iostream>

using namespace std;

int main()
{
const int numberOfPrimesToFind=10;

int discoveredPrimes[numberOfPrimesToFind];

discoveredPrimes[0]=2;
int noOfdiscoveredPrimes=1;

int numberUnderTest=3; // the remainder

bool isDivisible =0;
int i;
for(i=0;i<noOfdiscoveredPrimes;i++){
isDivisible = !(numberUnderTest%discoveredPrimes[i]);//! is an invert (just the opposite)
if (isDivisible) break;
}
if(!isDivisible){
discoveredPrimes[i]=numberUnderTest;
noOfdiscoveredPrimes++;
}

for (i=0; i<noOfdiscoveredPrimes; i++){
cout<<"Prime Numberis divisible by "<<i<<"= "<<discoveredPrimes[i]<<endl;
}

(i NEED HELP ON THIS PART BELOW)

while (i<=numberOfPrimesToFind; i++;)) {
cout<<"Number of Primes"<<i<<" ="<<numberUnderTest<<endl;

}

return 0;
}```
The instrcutions were

Write a program to find the first 10 prime numbers.
Hint: Prime numbers are numbers that can not be divided by
any other prime number other than 1.
Hint: Once you have determined that a number is prime,
store it in an array to try it for future tests.

Can someone help me with the while loop?? to make the numbers prime

2. You might also want to check out the contest forum, we had a prime number generating contest awhile back with some good algorithms.

As for your program, you need to reinitialize i before the while loop. A better option woudl be to use a for loop instead of that while loop, which looks liek an odd way to simply print out each member of an array.

3. Well, I'm not really sure what you were thinking when you wrote your code. There are numerous errors with it, including for-like semicolons inside while loops, if statements outside loops, access to an uninitialized array, etc.

That hint isn't particularly helpful, either, because you don't really need to store previous primes. I'm assuming it's referring to the use of an algorithm like this: Sieve of Eratosthenes - Wikipedia, the free encyclopedia

But if you're just trying to find the first ten prime numbers, you can use a nested for loop, the basic structure of which could look like this:
Code:
```loop until ten prime numbers are found
loop indefinitely on i from the last checked number
loop on j from 0 to the square root of i
if j divides i, this isn't a prime number, so start the i loop again```

4. Code:
```int c=0;
int zr=30; // this is the range for the  primenumber  search
for(int i=zr;i>0;i--)
{
c=0; // reset counter for the new number
for(int z=1;z<=zr;z++){
if(i%z==0){ // lets look if i % z = 0
c++;

}
if(c<=2&&z==i){
//if the number can only be devided 2 times  and we are at the end of the number range  we found a
//prime number  as u remember the prime can only be devided by 1 and itself
printf(" (%d) ",i);
break;
}

}

}```

```if(c>2){ //if c is bigger than 2 we dont need to look at rest of the number it cant be a prime