Next Prime Number Function C++

Hello Hello !
-I want a function which show me the first prime number after I press a regular int number.
1.For exemple I type 14 (N=14) then to show me the next prime number "only".
So I type 14,the prime number after this is 17.
2.Another exemple. I type N=2, I want to show me the next prime number which is

Let me show you a haw my code works now. It work fine till I press 14.
N=14 - resultPrime= 15
N=15 - resultPrime=17
N=16 - resultPrime=17
N=17 - resultPrime=19
N=18 - resultPrime=19

It still give me as prime numbers 15,19, maybe even more if I get more far, which is just wrong.

Here the Code Guys, Can You tell me what to change, I post on Beginners Topic cause I truly want to solve it as soon as possible:

#include<iostream>
using namespace std;
int FirstPrimeNumber(int N);
int isPrime(int nr);
int main(){
int N;
cout<<"N=";cin>>N;
cout<<"result="<<FirstPrimeNumber(N);

}
int FirstPrimeNumber(int N){
int nrNow; //curent number
nrNow=N+1;
for(;;){

if(isPrime(nrNow))return nrNow;
nrNow++;
}
}

int isPrime(int nr){
int i;
for(i=2;i<=nr/2;i++)
if(nr%i==0)return 0;
else return true;

}
Last edited on
Welcome to the forum! Thank you for being so clear with your question. You explained what the program should do, gave several examples, posted the incorrect output and included your code. These are exactly the things that will get you quick results.

The one thing that you could do better is to use code tags when posting your code. To do this, highlight the code while creating (or editing) the post and click the "<>" button on the right side of the edit window.

To answer your question, take a look at isPrime() when I indent the code to match the actual structure:
1
2
3
4
5
6
7
8
9
10
11
int
isPrime(int nr)
{
    int i;
    for (i = 2; i <= nr / 2; i++)
        if (nr % i == 0)
            return 0;
        else
            return true;

}

Notice that the "return true" is inside the loop. That means that isPrime() always returns on the very first time through the loop, depending on whether nr%i is zero.

To fix this, you want to return true only if you make it out of the loop:
1
2
3
4
5
6
7
8
9
10
11
int
isPrime(int nr)
{
    int i;
    for (i = 2; i <= nr / 2; i++) {
        if (nr % i == 0) {
            return 0;
        }
    }
    return true;
}


Some other suggestions:

Notice that I used braces even though there is only one statement inside the for loop, and even though there is only one statement inside the if. This ensures that if I decide to put another statement in there, the code will still work properly. Trust me, if you don't do this, you'll spend hours trying to track down a bug where you've indented the code one way, but the actual block structure is something different.

Use the for loop in FirstPrimeNumber:
1
2
3
4
5
6
7
8
int
FirstPrimeNumber(int N)
{
    for (int nrNow = N+1; ; mrNow++) {  // empty condition part is the same as "true"
        if (isPrime(nrNow))
            return nrNow;
    }
}


Your code is inefficient, but I suspect that efficiency wasn't the point of this exercise.
You're testing way more divisors than you need to.
You only need to go up to the square root of nr, not half of nr.
That's a big difference: sqrt(10000)=100 10000/2=5000
And the loop only needs to test the odd numbers if you test 2 separately.
1
2
3
4
5
6
7
8
bool isPrime(int nr) {
    if (nr < 3) return nr == 2;
    if (nr % 2 == 0) return false;
    for (int i = 3; i * i <= nr; i += 2)  // or calc sqnr = sqrt(nr) and use i <= sqnr
        if (nr % i == 0)
            return false;
    return true;
}

The FirstPrimeNumber loop would only need to pass odd numbers, too, handling 2 separately if needed.
Last edited on
Thank you soo much guys....someone from other post solved me....Wish you all best. I wanna check your solution as well....I really love knowing different ways when solving things. Sry for me english
Topic archived. No new replies allowed.