Modulus with floats

Discussion in 'C++' started by tonydav43, Aug 10, 2011.

  1. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    I am working my way through the C++ without fear new edition, and have found some codes that dont work. This is a simple code to work out if a number is prime using the square root function and modulus. Here is the code as per the book:

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    
    {
    	int n;
    	int i;
    	int isPrime = true;
    
    	cout << "Please enter a number and press enter " ;
    	cin >> n;
    	cout << endl;
    
    	i = 2;
    
    	while (i <= sqrt([COLOR="Magenta"]double[/COLOR](n)))// or (i*i<=n)
    	{
    		if (n % i == 0)
    		
    			isPrime = false;
    		i++;
    		}
    		
    	
    	if (isPrime)
    		
    		cout << "Number is a prime number" << endl;
    
    	else
    
    		cout << "Number is not a prime number" << endl;
    
    }
    
    
    The word double which is in red, is what I had to add to get past the errors stating that the compile was unsure if it was supposed to return a float double etc.

    Now the next part of the exercise was to take the square root function out of the loop, and declare a new variable as the result of the square root of the users input, then get the function to check it if was a prime number, here is how far I got

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    
    {
    	float m;
    	int n;
    	int i = 2;
    	int isPrime = true;
    
    	cout << "Please enter a number and press enter " ;
    	cin >> n;
    	cout << endl;
    
    	m = sqrt (float (n));
    
    	cout << m;
    	cout << endl;
    
    
    	while (i <= m ) // or (i*i<=n)
    	{
    		if (m % i == 0)
    		
    			isPrime = false;
    		break;
    		}
    	i++;
    
    	if (isPrime)
    		
    		cout << "Number is a prime number" << endl;
    
    	else
    
    		cout << "Number is not a prime number" << endl;
    
    }
    
    
    I now get the error message error C2296: '%' : illegal, left operand has type 'float'. So how do I now complete this exercise, taking into account I am only on chapter 2 of the book
     
    Last edited by a moderator: Aug 11, 2011
  2. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    I think I have managed to solve this but want to make sure


    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    
    {
    	int n;
    	double m;
    	int i = 2;
    	int isPrime = true;
    
    	cout << "Please enter a number and press enter " ;
    	cin >> n;
    	cout << endl;
    
    	m = sqrt(double(n));
    
    	cout << m;
    	cout << endl;
    
    
    	while (i <= m) // or (i*i<=n)
    	{
    		if (n % i == 0)
    		
    			isPrime = false;
    		break;
    		}
    	i++;
    
    	if (isPrime)
    		
    		cout << "Number is a prime number" << endl;
    
    	else
    
    		cout << "Number is not a prime number" << endl;
    
    }
    
    So what this is doing if I am correct, is calculating the square root of int n only once, then as long as double m is less than or equal to int i, it will keep looping, is this code right ?????
     
    Last edited by a moderator: Aug 11, 2011
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well,

    1. What does the program print if you run it? Give it some input, say 9, maybe run it a few times with other numbers, and see what it displays.

    2. As for what it does after calculating and displaying the square root of n, does the variable name "isPrime" give you a clue? If not, does the output "Number is a prime number" give you an even bigger clue?
     
  4. tonydav43

    tonydav43 New Member

    Joined:
    Aug 10, 2011
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    I was not asking what it outputs, and yes I know it is a prime number, if you read the question I was asking if the code only calculated the square root once. If I placed the sqrt part of the code in the function, it will calculate the square root each time it is run and i is incremented, I was purely asking if by placing the input outside the function, does it only calculate the square root once. You have not read my question correctly.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, OK, then the question is ambiguous:
    First part "calculating the square root of int n only once", to which the answer is yes.

    Second part "then as long as double m is less than or equal to int i, it will keep looping". Yes, in a sense, but what I thought you were asking was about the higher level functionality, i.e. what is it actually doing rather than how is what it is doing implemented, which is that it is determining if the number is prime, using the square root it previously calculated and displayed.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice