#include <iostream>
#include <cmath>
using name space std;
int prime(int n);
int main() {
int i;
while(1) {
cout << "Enter a number (0 to exit)";
cout << "and Press ENTER:";
cin >> i;
if (i == 0)
break;
if (prime(i))
cout << i << " is prime\n";
else
cout << i << " is not prime\n";
}
return 0;
}
int prime(int n) {
int i;
for (i = 2; i <= sqrt((double) n); i++) {
if (n % i == 0)
returnfalse;
}
returntrue;
}
This is a prime test using function rather than those junky statements in the last example. But what comes to mind is that since the author or the book uses the while infinite loop, I am just wondering WHAT IS THE ADVANTAGE of DOING THAT? and in addition to the infinite while loop, is it possible to get rid of it, just by using the codes like this:
1 2 3 4 5 6 7 8 9
cout << "Enter a number (0 to exit)";
cout << "and Press ENTER:";
cin >> i;
if (i == 0)
break;
if (prime(i))
cout << i << " is prime\n";
else
cout << i << " is not prime\n";
Last question is regarding the function definition placed in the last part of the code. Well...Need some explanation here and it would be better if there are some details.
1 2 3 4 5 6 7 8
int prime(int n) {
int i;
for (i = 2; i <= sqrt((double) n); i++) {
if (n % i == 0)
returnfalse;
}
returntrue;
}
Instead of returning false and then returning true, is it ok to do that:
1 2 3 4 5 6 7 8 9
int prime(int n) {
int i;
for (i = 2; i <= sqrt((double) n); i++) {
if (n % i == 0)
returnfalse;
}
elsereturntrue;
}
No...if you looked at the code you would see that they use the while loop so you can input as many numbers as you want. Otherwise, you would only get to input one number.
No, because you put the else where there is no matching if to go with it. Even if you moved into the loop, it would return after the first loop always, which means that even if you put in 9 for example, your edited program would return that is was prime.
Sorry! I am just a bit confused about what you said and hope you could explain it a little bit more in details. Besides, some of the people said if we use the else return true, it is not the matching statement that goes with the if-statement you mentioned earlier.
But the thing is since we could use syntax as below:
if (condition)
statement;
Isn't ok to use else statement as well? So how is it mismatched, sounds stupid but hope yo could explain and illuminate me in this aspect. Besides, what is an alternative for the while loop? And how is the input you mentioned about with the while loop, we could have as many input of numbers as we want? Confused here as well.
for (i = 2; i <= sqrt((double) n); i++) {
if (n % i == 0) //this is inside the loop
returnfalse;
}//end for loop
else //no if match the else, since this is outside the loop
returntrue;
If you have good indentation you can see these errors a lot easier.
And as for the while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while(1) {
cout << "Enter a number (0 to exit)";
cout << "and Press ENTER:";
cin >> i;
if (i == 0) //if they input 0
break; //leave the while loop
if (prime(i)) //check the function
cout << i << " is prime\n";
else
cout << i << " is not prime\n";
}
//here we will go back to the start
}
return 0; //if we break out of the while loop it will end
So if the else statement is within the loop, is it possible to use it right?
1 2 3 4 5 6
for (i = 2; i <= sqrt((double) n); i++) {
if (n % i == 0) //this is inside the loop
returnfalse;
else // match the else, since this is inside the loop
returntrue;
}//end for loop
I really WANNA KNOW HOW THE RETURN STATEMENT CAN BE USED. Can we just use it to return anything? Even JUST return true?