problem with while loop and sqrt

Hello everyone. This is my first post here and first time programming C++. I am learning from a book and can't get this example to compile. It is supposed to determine whether or not a number entered by the user is prime or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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; // Boolean flag...

        // 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;
        while (i <= sqrt(n)) { // While i is <= sqrt(n),
                if (n % i == 0) // If i divides n,
                is_prime = false; // n is not prime.
                i++; // Add 1 to i.
        }
        
        // Print results
        if (is_prime)
        cout << "Number is prime." << endl;
        else
        cout << "Number is not prime." << endl;
        
        system("PAUSE");
        return 0;
}


The error I am getting says: "call of overloaded 'sqrt(int&)' is ambiguous" on line 17, which is the one with the "while" expression.

Sorry for my newbie question and thank you in advance for any help! :)
P.S. I am using Dev-C++ 4.9.8.0
There are several sqrt functions. One for float, double and long double. There is not sqrt for int. Normally the compiler would perform a cast, but now it doesn't know whether to cast to float or double. You need to cast it yourself: i <= sqrt((float)n) (there are several other ways to write this, like float(n), static_cast<float>(n) or even n*1.0//this casts results in double, write 1.0f for float .)

By the way, a faster way to write the same thing would be i*i <= n.
Last edited on
Thank you so much! I couldn't get it to work with i <= sqrt((float)n) but i*i <= n works great and makes more sense! :) Any idea why it won't work the other way, though? Is it because i is declared as an int at the top? Here is the code and my errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
using namespace std;

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

    // 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;
        while (i <= sqrt((float)n) { // While i is <= sqrt(n),
                if (n % i == 0) { // If i divides n,
                is_prime = false;
                break;
                } // n is not prime.
                i++; // Add 1 to i.
        }
        
        // Print results
        if (is_prime)
        cout << "Number is prime." << endl;
        else
        cout << "Number is not prime." << endl;
        
        system("PAUSE");
        return 0;
}


Error: line 17: parse error before `{' token
Last edited on
Your problem is that you accidentally deleted one ) right before { on line 17.
closed account (1vRz3TCk)
You would also do well to not have sqrt(n) in the expression of the while loop.

It would be better to have
limit =  sqrt(n)
while i <= limit

as you only need to calculate the limit once, not on every iteration of the loop.
Thank you both! :)
There is not sqrt for int.

There is one now, since 2011, but yes, for old compilers, explicit cast is what's required if you really need to call std::sqrt().
Last edited on
You can use this code to check for the number whether it is prime or not
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// prime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int num;
	
	
	char ch;
	do
	{
	int factors=0;
	cout<<"Enter a number ";
	cin>>num;
	for(int i=1;i<=num/2;i++)
	{
		if(num%i==0)
		{
			factors++;
		}
		
		

	}
	if(factors>1)
		cout<<num<<" is not a prime number"<<endl;
	else
		cout<<num<<" is a prime number"<<endl;

	cout<<"Do you want to do it again ";
	cin>>ch;
	}
	while(ch=='y' || ch=='Y');
	return 0;
}

Topic archived. No new replies allowed.