DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Having a problem with calling the sqrt function trying to pass an integer

Threaded View

  1. #1
    Join Date
    Sep 2019
    Posts
    11

    Having a problem with calling the sqrt function trying to pass an integer

    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;
    }
    Last edited by 2kaud; 09-10-2019 at 04:17 AM. Reason: Added code tags

Similar Threads

  1. SQRT lookup, slower than sqrt()?
    By pirateninja in forum C++
    Replies: 3
    Last Post: 06-18-2007, 08:43 AM
  2. Replies: 5
    Last Post: 11-23-2005, 10:10 AM
  3. Problem calling a function from asp.net
    By xxxxx in forum ASP.NET
    Replies: 2
    Last Post: 07-28-2005, 03:44 AM
  4. Replies: 0
    Last Post: 07-21-2000, 08:36 AM
  5. Replies: 0
    Last Post: 07-21-2000, 06:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center