#include <iostream>
#include <cmath>
usingnamespace std;
bool prime(int n);
int main() {
int i;
while (true) {
cout << "Enter num (0 = exit) and press ENTER: ";
cin >> i;
if (i == 0)
break;
if (prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
system("PAUSE");
return 0;
}
bool prime(int n) {
int i;
double sqrt_of_n = sqrt(n);
for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
if (n % i == 0)
returnfalse;
}
returntrue;
}
Never have I rewritten main before but a for loop is a hint so I'm guessing:
#include <iostream>
#include <cmath>
usingnamespace std;
bool prime(int n);
int main() {
int i;
for (int n = 20; n <= i; n++) {
cout << "Enter num (0 = exit) and press ENTER: ";
cin >> i;
if (i <= 2)
break;
if (prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
system("PAUSE");
return 0;
}
bool prime(int n) {
int i;
double sqrt_of_n = sqrt(n);
for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
if (n % i == 0)
returnfalse;
}
returntrue;
}
Won't compile, also this isn't changing the main??
In this version, you should delete lines 13 to 17, they aren't needed here.
I've no idea what n is at line 11.
Since your loop is counting down from 20, you need to test for i being greater than some limiting value (in this case you want the last value to be 2).
Though I thought from the original description that the loop should count upwards, starting at 2, and end with 20.
#include <iostream>
#include <cmath>
usingnamespace std;
bool prime(int n);
int main() {
int i;
for (int i = 20; 2 <= i; i++) {
cin >> i;
if (i <= 2)
if (prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
system("PAUSE");
return 0;
}
bool prime(int n) {
int i;
double sqrt_of_n = sqrt(n);
for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
if (n % i == 0)
returnfalse;
}
returntrue;
}
The cin at line 13 is not needed.
The for loop should start with the first value, in this case 2, you have the 20 and 2 the wrong way around.
Looks like you need to think carefully about the comparison condition in the for loop. You need the loop to continue while this condition is true, and stop as soon as it become false.
#include <iostream>
#include <cmath>
usingnamespace std;
bool prime(int n);
int main() {
int i;
for (int i = 2; i <= 20; i++) {
if (prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
system("PAUSE");
return 0;
}
bool prime(int n) {
int i;
double sqrt_of_n = sqrt(n);
for (i = 2; i <= sqrt_of_n; i++) { // We can see sqrt(n) is calculated every iteration here in the check statement
if (n % i == 0)
returnfalse;
}
returntrue;
}
I think you're missing something here. A high-level understanding of what it is your code is supposed to do, and the purpose of each line.
Let's take the original program and strip the main() down to the bare minimum.
1 2 3 4 5 6 7 8 9
int main()
{
int i = 7;
if (prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime" << endl;
}
At line 3 an arbitrary value of 7 is assigned to the variable i. (You could choose any positive value).
All the other lines in the original were to help get different values for i, by asking the user to type in the value.
Now we need to use a for loop to supply those different values. So all that is needed inside the loop is simply those four lines of the if-else statement.
The for loop is still not quite right, at the start i will be equal to 2.
Now, is 20 <= 2 true or false? Well, 20 isn't less than 2. And 20 is not equal to 2. So it is false.
You need to change this condition so it is true for the range 2 to 20.