| princesslumy (36) | |
|
How to make the program run properly?? #include<cstdlib> #include<iostream> #include<cmath> #include<cctype> using namespace std; int a,num; int isprime(int a) { int n=0; int num=0; for (int i=1;i>=0;i++) { num++; for (int j=2;j>=100;j++) if (num%j==0 && num!=j) return (0); else if (num%j==0 && num==j) n++; } if (n==a) return(n); } int main () { cout<<"Enter a positive integer: "; cin>>a; while (a<=0) { cout<<"Enter a positive integer: "; cin>>a; continue; } cout<<"the "<<a<<" th prime number is: "<<isprime(num)<<endl; system("pause"); return 1; } | |
|
|
|
| nixer526 (17) | |||
|
This isn't the best way to do it, but it should give you an understanding... Personally I would also like to learn a better way of doing it, so if anyone else can improve the code, please feel free to do it.
| |||
|
|
|||
| fg109 (131) | |||
|
From the title, I thought the problem is finding the Nth prime. As in "Find the 10th prime" (29). I am not exactly sure what the OP's code is supposed to do, and nixer526's code is just checking whether the number entered is a prime or not. Many of the Project Euler problems deal with prime numbers, and this is the code I came up with for one of them:
Lines 39 to 68 is my implementation of the Sieve of Eratosthenes. It is basically me storing the primes up to 1000000 inside a vector called Primes. | |||
|
Last edited on
|
|||
| nixer526 (17) | |
|
I thought the OP ment to look for the next prime number that he entered... So for example, if he entered 13 and was looking for the next prime number, it would be 17... I might be wrong though. Thanks for the share fg109. Your's seems to be what the OP might be requesting. | |
|
|
|