Thread: Prime or not prime (i need help)

1. Prime or not prime (i need help)

Code:
```int main()
{
int N, C;
cout<<"Enter an integer ";
cin>>N;
C=2;
while(N>C*C&&N%C!=0)
{
C=C+1;
}
if(N>=C*C)
cout<<"NOT PRIME"<<endl;
else
cout<<"PRIME"<<endl;

system("PAUSE");
return 0;
}```
This is the code which finds the integer if its prime or not. But i need to do something different. I will enter 15 integers and the program will print only the prime ones.

I have coded this but didn't work.

Code:
```int main()
{
int A[15];
int N, C;
for(N=0;N<15;N=N+1)
{
cout<<"Enter "<<N+1<<". value: ";
cin>>A[N];
}
cout<<"--------"<<endl;
C=2;
while(N>C*C&&N%C!=0)
{
C=C+1;
}
if(N>=C*C)
{
cout<<A[N]<<endl;
}

system("PAUSE");
return 0;
}```
Can anyone help me how can i do this program.

2. What about something like this:
Code:
```for i=0 to 14
enter number i and store in array[]

for(i = 0 to 14)
if(isPrime(array[i]))
print out the number```
Basically I think you should make the code that determines if the number is prime a function that returns a boolean value. Read in your array of values and then call the function to dtermine if it's prime. If it is, then print it out. Otherwise, ignore it.

Hope this helps.

Also you shouldn't use system("pause") change it to

cin.ignore();
cin.get();

3. thanks for your help but i didnt understand : (
can you write down the full code with correction

4. All you need to do is put this loop
while(N>C*C&&N%C!=0)

Inside this loop
for(N=0;N<15;N=N+1)

Or as an alternative, put this loop
while(N>C*C&&N%C!=0)
inside another function, called say isPrime() which you pass a single integer to, and it returns a true/false value.

5. I can't do

7. Or search the forums! I'm sure there was something about this on the boards. Look through here

8. Originally Posted by toggie
I can't do