I'm trying to pick out whether or not a prime number is given by a user, when they input a number. This is what I have, but it' not working correctly....can anyone suggest anything?

#include <iostream>
using namespace std;

void main()
{
	int n;

	while (true)
	{
	cout << "Enter an integer: ";  cin >> n;
	if (n!=2 && (n%2==0 || n%3==0 || n%5==0))
		cout << "Not Prime" << endl;
	else
		cout << "Prime" << endl;
	}
	cout << endl;

}

make a separate function.

bool isPrime(int i) { /* logic here */ };

//somewhere inside main
if( isPrime(i)  == true ) cout <<" Prime \n" ; 
else cout <<" not a prime\n";

Of course this code depends on isPrime function. You should try to create that on your own. If you have trouble, either google or ask.

You need to use a loop and divide the number n by each odd number from 3 to n/2. If any number divides n evenly (no remainder) n is not prime.

Actually you only need to loop through n^(1/2) [The square root of N].
Must include Math.h

bool isPrime(int i)
{
int t;
double root = sqrt((double)(i));

for (t = 0; t<root; t++)
{
  if(i%t==0) return false;
}
return true;
}

Go ahead and try this out. I didn't compile it, but wrote something similar the other day. Good luck.

Edited 7 Years Ago by Campbel: n/a

Waltp and Campbel are right in their concepts but you can still decrease the numbers from which you divide.

If the number you need to check is "num" then divide num with 2 and every odd number form 3 upto [ ( int ) sqrt ( num ) + 1 ] (both inclusive) this decreases the count of candidate numbers more than n/2 and yet still gives the answer.

@Campbel : you are majorly wrong in your program , your candidate numbers start from 0(loop starts form 0).This is the biggest mistake you have done.
1)It would cause an overflow when a number is divided by 0.
2)Every number is divisible by 1 hence it would never show a number to be prime.

Good effort but show a bit of caution when guiding others. :)

Edited 7 Years Ago by csurfer: n/a

CSurfer is right, and I learned this when I made my first prime number function. Sorry for the bad code. Next time I'll run it through the compiler :)

This code should be good.

bool isPrime(int i)
{
int t = 2;
double root = sqrt((double)(i));
if(i%t==0) return false;
t++;
while(t<root)
{
if(i%t==0) return false;
t+=t+2;
}
return true;
}
Comments
For twice giving the answer and not letting the OP figure it out himself

I'm trying to pick out whether or not a prime number is given by a user, when they input a number. This is what I have, but it' not working correctly....can anyone suggest anything?

#include <iostream>
using namespace std;

void main()
{
	int n;

	while (true)
	{
	cout << "Enter an integer: ";  cin >> n;
	if (n!=2 && (n%2==0 || n%3==0 || n%5==0))
		cout << "Not Prime" << endl;
	else
		cout << "Prime" << endl;
	}
	cout << endl;

}

Hey there!
Don't forget to look at the replies in your other thread where you've already asked about this!
http://www.daniweb.com/forums/thread230230.html

After you got an answer to your original question about odd numbers, you went on to ask about primes!

Cheers for now,
Jas.

This article has been dead for over six months. Start a new discussion instead.