CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: help me

  1. #1
    Join Date
    Oct 2012
    Posts
    9

    help me

    I don't know what should I doing. About cmath. Here

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main() {
        int n;
        int i;
        int is_prime = true;
        
        cout << "Enter a number and press Enter: ";
        cin >> n;
        
        i = 2;
        while (i <= sqrt(n)) {
            if (n % i == 0)
                is_prime = false;
            i++;
        }
        
        if (is_prime)
            cout << "Number is prime." << endl;
        else
            cout << "Number is not prime." << endl;
            
        system("pause");
        return 0;
    }
    Please fix it for me. Thanks

  2. #2
    Join Date
    Oct 2012
    Posts
    9

    Re: help me

    I mean, Number 15 is error. It is said that C:\Users\Duy N. Le\Desktop\C++\prime1.cpp
    call of overloaded `sqrt(int&)' is ambiguous

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: help me

    Check the documentation for sqrt and see what type of argument it expects. Compare that to what you're passing it.

  4. #4
    Join Date
    Oct 2012
    Posts
    17

    Re: help me

    Do you have to use the sqrt() function?
    Here's a code I did a while ago, it lists prime numbers from a given range.
    It can be modified to just tell if a number is prime.
    Hope it Helps
    Code:
    // Prime Number Notes 10-7-2011.cpp : main project file.
    #include "stdafx.h"
    #include <iostream>
    #include "resource.h"
    
    using namespace std;
    
    void DisplayEvens(int, int);
    
    int main()
    {
    	// Declare and init two intergers
    	// Beginning num and ending numbers
    	int intBeginNum = 0;
    	int intEndNum = 0;
    
    	// Prompt user for range 
    	cout << "Enter a beginning number for the range:" << endl;
    	cin >> intBeginNum;
    
    	cout << "Enter an ending number for the range:" << endl;
    	cin >> intEndNum;
    
    	DisplayEvens(intBeginNum, intEndNum);
    
        system ("pause");
    	return 0;
    }
    void DisplayEvens(int intBeginNumDE, int intEndNumDE)
    {
    	// Initialization and declare
    	// this is done inside of for loop
    	
    	bool bolPrime = false;
    
    	for (int i = intBeginNumDE; i <= intEndNumDE; i++)
    	{
    		
    		for (int j = 2; j < 10; j++)
    		{
    			if (i != j && i != 1)			
    			{
    				if (i % j != 0)
    					bolPrime = true;
    				else
    				{
    					bolPrime = false;
    					break;
    				}
    			}
    		}
    		if (bolPrime) 
    			cout << "Prime Number: " << i << endl;
    	}
    	return;
    
    }

  5. #5
    Join Date
    Oct 2012
    Posts
    9

    Re: help me

    Never mine. I already problem solved because I found out it. Show it here.

    Code:
    (i <= sqrt((double)n))
    It would worked. :-)

  6. #6
    Join Date
    Oct 2012
    Posts
    17

    Re: help me

    Alright kool.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured