sqrt c++ problem, anyone?

In my code (shown below) I have a problem with "sqrt". (I'm a beginner in C++, started yesterday)
For some reason, I can't compile because "sqrt" is having an error.
(This project is testing each number to tell if it's prime or not. [I got this straight from a book: C++ Without Fear: Second Edition])
So yea, the error is: "more than one instance of overloaded function "sqrt" matches the argument list"
Could someone please tell me how to fix this?

#include <iostream>
#include <cmath>
using namespace std;

int main(){
int n; //Number to test for prime-ness.
int i; //Loop counter
int is_prime = true;

//Get a number from the keyboard.

cout <<"Enter a number and press ENTER: ";
cin >> n;

//Test for prime by checking for divisibility
//By all whole numbers from 2 to sqrt(n).

i = 2.0;
while (i <= sqrt(n)){
if (n % i == 0);
is_prime = false; // n is not prime

//Print results

cout<<"Number is prime." << endl;
cout <<"Number is not prime." << endl;

return 0;

The problem is that the sqrt function does not take an integer as a argument. You will want to do

sqrt((double)n) or sqrt((float)n) to fix it.
Last edited on
Ok, that fixed my problem. Thanks man! :D
Topic archived. No new replies allowed.