find nth prime number

Write your question here.
Hi all,
I am trying to test weather a number is prime, then find the nth prime number.I am stuck. Please help!
What do you have so far?
What do you have so far?
Last edited on
#include<iostream>
using namespace std;
int isPrime(int num){
int nPrime = num + 1;
int pCounter = 0;
bool primeFound == false;
while (primeFound == false){
for (int i = 2; i < nPrime; i++){
if (nPrime %i == 0){
pCounter++;
break;
}
}
if (pCounter == 0){
primeFound = true;
pCounter = 0;
return nPrime;
}
else {
nPrime++;
pCounter = 0;
}
}
return 0;
}

int main(){
int a;
cout << "type a positive integer" << endl;
cin >> int num;
cout << "F: Find the nth prime number" << endl;
cout << "T: Test a value to see if it is prime" << endl;
cout << "Q: Terminate the program" << endl;
cout << "Next Step (F/T/Q)" << endl;

}
Your isPrime function is much too complicated. Separate different parts of into different functions. One to find if a number is prime. One to find the Nth prime. (It can include isPrime).

All isPrime needs to do is check if a number is prime. So it should be a bool returning true or false depending on wether or not the number is prime.

P.S. You never actually call isPrime().
P.P.S. Please use code tags.
Why the double post? -> confusing.
http://www.cplusplus.com/forum/beginner/201324/
Last edited on
Topic archived. No new replies allowed.