# 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
Please, please, please put the loop inside another you said and give me the full code

6. While some of us may be code machines or code monkies, we aren't here to do your work for you. Read your book, ask your teacher, use google.

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
Please, please, please put the loop inside another you said and give me the full code
NO, we will NOT do this for you! This programming task is obviously too far above your level of understanding.

You should fail this otherwise it will give the teacher the false impression that you have the slightest idea what you are doing, when it is very clear that you don't!

9. ok i did it. thanks.

10. In case you want to see a program which outputs first 1000 prime numbers (using prime test function) -- here is an example:
http://www.anyexample.com/programmin...lity_test).xml

There is also a little more efficent function for prime testing.

Popular pages Recent additions