# Thread: Little help on small problem

1. ## Little help on small problem

What I'm trying to do is have a user enter a number
have the computer check to see if it is prime number. AND if it is not prime what is the next the next closest prime number. i got the figuring out the prime number part but don't know how to add in the additional info needed (the next closest prime number). Here is what I got so far

if ( n%2 == 0 ) {
# The number is even, so not prime.
}
else {
for ( i = 3; i < sqrt(n); 2++ ) { # Check odd divisors only
if ( n%i == 0 ) {
# The number is not prime.
}
}
}
# The number is prime.
Any ideas
Thanks
Matt

3. What I'm trying to do is have a user enter a number
have the computer check to see if it is prime number. AND if it is not prime what is the next the next closest prime number. i got the figuring out the prime number part but don't know how to add in the additional info needed (the next closest prime number). Here is what I got so far
Code:
```if ( n%2 == 0 ) {
# The number is even, so not prime.
}
else {
for ( i = 3; i < sqrt(n); 2++ ) { # Check odd divisors only
if ( n%i == 0 ) {
# The number is not prime.
}
}
}
# The number is prime.```
Sorry buddy

Thanks
Matt

4. One thing I can think of is that you place the algorithm in a function and you have the value as a parameter and then if the first number isnt a prime call that function with n+1 as parameter. You then simply return the prime number.

Oh and by the way, you REALLY only want to call sqrt once for every number to check, otherwise the algo will be slow.

One last thing, you should get a habit of indenting your code, let me show you a small example:

Code:
```#include <iostream>
using namespace std;

int main()
{
for(int i=0; i<10; i++)
{
cout << "This is unindented code and is hard to read" << endl;
cout << "With more code it will be very hard to read if not indented" << endl;
}
cout << "So get the habit of indenting." << endl;
}```
Code:
```#include <iostream>
using namespace std;

int main()
{
for(int i=0; i<10; i++)
{
cout << "This is indented code and is easy to read" << endl;
cout << "You will also catch errors such as missing a closing bracket more easily." << endl;
}
cout << "So get the habit of indenting." << endl;
}```
See the difference