![]() |
Modulus with floats
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>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> |
Re: Modulus with floats
I think I have managed to solve this but want to make sure
Code:
#include <iostream> |
Re: Modulus with floats
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? |
Re: Modulus with floats
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.
|
Re: Modulus with floats
Well, OK, then the question is ambiguous:
Quote:
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. |
| All times are GMT +5.5. The time now is 21:56. |