0

i know how to find prime numbers of several numbers, does anyone know hoew to find a prime sumber of a single number inputed by the ser using the following function and printing out if the number is prime

```
int mai()//asks for input
{
int num;
cout << "enter number:";
cin >> num;
call IsPrime;
cout if prime or not
}
bool IsPrime(int n)
// precondition: n >= 0
// postcondition: returns true if n is prime, else returns false
// returns false if precondition is violated
{
if (n < 2) // 1 and 0 aren't prime
{ return false; // treat negative #'s as not prime
}
else if (2 == n) // 2 is only even prime number
{ return true;
}
else if (n % 2 == 0) // even, can't be prime
{ return false;
}
else // number is odd
{ int limit = int(sqrt(n) + 1); // largest divisor to check
int divisor = 3; // initialize to smallest divisor
// invariant: n has no divisors in range [2..divisor)
while (divisor <= limit)
{ if (n % divisor == 0) // n is divisible, not prime
{ return false;
}
divisor += 2; // check next odd number
}
return true; // number must be prime
}
}
```

any suggestions would greatly be appreciated

*Edited 3 Years Ago by Dani*: Formatting fixed