Need to find larger prime number then has been inputed.how to do that: I cin 5 the reult it self should be 7...?

## All 7 Replies

I'm a bit confused on what you want, do you want to output a list of prime numbers whose range is upto the input or check if the input is a prime number or something else entirely?

not a list but 1 prime number which is higher then inputed number

Ok, so you need, if, for example, 5 is inputed, to show you 7, and if 7 is inputed to shouw you 11, and so on and so forth.
Well, we'll need a proof that you tried to solve your problem, so that we know where you're stuck. Post what you've done so far:).

*

``````i'm not stuck enywhere, just need to know formula or smth,i'm using just prime numbers identification
for (i = 2; i<=sqrt(n); i++)}
if (n % i == 0); // If i divides n evenly,*
``````

...and ftw with this rich text box- why it marks all the sentence i wrote... and can't sometimes edit in here?

well you need to use a loop and go from the next odd number greater than what you entered untill you find a number that is prime.

``````bool prime = false;
while(!prime)
{
// check if number is prime
// if number is prime set prime to true
// increment number to check
}
``````

Here's something I found on Google:

``````bool checkPrime(int num){
if(num<=1) return false;
if(num==2) return true;
if(num%2==0) return false;
for(int i=3; i<=sqrt(num*1.0); i+=2)
if(num%i==0) return false;
return true;
}
``````

Also, if you want to get the next prime number, you could do it like this:

``````while(++nr && !checkPrime(nr));
``````

And after the loop ends, you'll have your next prime number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.