I'm having a problem calling the sqrt function in my prime number checker program. My intention is to make the for loop only check up to the square root of the number the user has entered. But C++ Builder is telling me there is ambiguity between sqrt(double) and sqrt(long double).
I want to pass an integer to the sqrt function but I don't know if I can. Cast it to an int perhaps?
Here is what I got:
Code:
#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;
bool isPrime(int num);
int main()
{
int x;
bool flag;
cout << "Enter a positive integer: ";
cin >> x;
flag = isPrime(x);
if (flag == true)
cout << x << " is a prime number" << endl;
else
cout << x << " is not a prime number" << endl;
getch();
return 0;
}
bool isPrime(int num)
{
bool flag;
for (int i = 2; i <= sqrt(num); i++)
if(num % i == 0)
{
flag = false;
break;
}
return flag;
}