Modulus with floats

tonydav43's Avatar, Join Date: Aug 2011
Go4Expert Member
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(double(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 shabbir; 11Aug2011 at 13:26.. Reason: Code blocks
tonydav43's Avatar, Join Date: Aug 2011
Go4Expert Member
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 shabbir; 11Aug2011 at 13:26.. Reason: Code blocks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
tonydav43's Avatar, Join Date: Aug 2011
Go4Expert Member
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.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Well, OK, then the question is ambiguous:
Quote:
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 ?????
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.