| princesslumy (36) | |
|
Hi guys am new in the forum. I need help in compiling a program that gives the next prime number example if someone says 5 then the program will give the next prime number 7 This is what i have so far, but is not successful. I will appreciate any help. Thanks in advance #include<iostream> #include<string> #include<cmath> #include<iomanip> #include<cstdlib> using namespace std; int isPrime( int, int, int, int, int, int); int num, b, c, d, x, y; int main() { cout << "Enter an integer = "; cin >> num; isPrime (num, b,c,d,x,y); cout << "The factors are " << endl; if(num!=0) { for(int y=1, d=0; y<=abs(num); y++) { c=num%y; b=num/y; } if(c==0&&abs(b)>=sqrt(abs(num))) { x=y*b+d; d=x; cout << y << " and " << b << endl; cout << -y << " and " << -b << endl; } else; } if((x*x==num*num)&&(abs(num)!=1)) { cout << num << " is a prime number." << endl; } else { cout << num << " is not a prime number." << endl; } system("PAUSE"); return 0; } int isPrime(int num,int b, int c, int d, int x, int y) { for(int i= num; i >= y ; i++) cout << "The N-th prime number is : " << endl; return 0; } | |
|
Last edited on
|
|
| Gulshan Singh (46) | |
|
First of all format your code using the <> button so it's easier to read. I'll lay out the general idea. Start at the input number + 2 (assuming the input number will always be prime, not sure from your description). Call a function called is_prime() that determines if the number is prime or not. If not, increment the input number by one and repeat the loop. is_prime() will work like this. Set i = 2. While i * i < input, check if (input % i) ==0. If it does, then return false. If it doesn't then return true.
| |
|
|
|