Hi,
im trying to write a program to decide this number is prime or not, and keep continue till user enter 0.also I need to exclude1 and 2 from the program.
I could write this, it does work ok in the first loop but second loop tart showing wrong results.
[code]
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Welcome to the wiz-bang prime number thing!\n";
cout << "Enter an integer greater than 2(0 to quit) ";
cin >> num;
if (num > 2)
{
if (num < 10)
{
for (int i = 2;i <= num;i++)
{
if (num == 3 || num == 5 || num == 7)
{
cout << num << " is prime.\n";
;
}
if (num == 4 || num == 6 || num == 8 || num == 9)
{
cout << num << " is not prime.\n";
}
cout << "Enter an integer greater than 2(0 to quit) ";
cin >> num;
}
}
if (num >= 10)
{
for (int i = 2;i <= num;i++)
{
if (num%i == 0)
{
cout << num << " is not prime.\n";
}
if (num%i != 0)
{
cout << num << " is prime.\n";
}
cout << "Enter an integer greater than 2(0 to quit) ";
cin >> num;
}
}
}
if (num == 0)
cout << "Thanks for playing.\n";
#include <iostream>
usingnamespace std;
bool primarity_test(int num);
int main(){
int num(1);
cout << "Welcome to the wiz-bang prime number thing!\n";
cout << "Enter an integer greater than 1(0 to quit) ";
cin >> num;
while (num!=0){
if(num==1){
cout << "you input 1. Retry." << endl;
cin >> num;
continue;
}
if(primarity_test(num)){
cout << num << " is prime!" << endl;
} else {
cout << num << " is not prime!" << endl;
}
cout << "Enter an integer greater than 1(0 to quit) ";
cin >> num;
}
cout << "Thanks for playing.\n";
}
bool primarity_test(int num){
if (num==2) returntrue;
for(int i=2; i<num; i++){
if (num%i==0) returnfalse;
}
returntrue;
}
i.e., since you are going to use your primality test all the time, put it in a function; if you mean to run a loop you should use a loop; since you use so many if, why not test for 2???
EDIT:: in your code, you wrote the wrong condition in for (int i = 2;i <= num;i++)
in fact if you input 5 and you test 5%5 this is 0 even if 5 is prime. you shall test for for (int i = 2;i < num;i++)
bool is_prime(int n) {
if (n < 2) returnfalse;
if (n == 2) returntrue;
if (n % 2 == 0) returnfalse; // handle evens separately
for (int i = 3; i * i <= n; i += 2) // only go up to the square root of n
if (n % i == 0)
returnfalse;
returntrue;
}
thank you so much, the reason i didnt use bool is i really dont understand how bool works tbh and make me confused. also why we use this: i * i <= n; i += 2 ??!!
We're using i += 2 to increment the iterating variable by 2. It's the same as i = i + 2
We might as well increment the iterating variable by 2 here because we know for a fact that even numbers are multiples of 2. So if n were to be divisible by either of these numbers then it would be divisible by 2 (we have established by the statement if (n % 2 == 0) returnfalse; that the number isn't divisible by 2).
So instead of checking 3,4,5,6,7,8,9 etc. we check 3,5,7,9 etc.
-> It doesn't make much difference because computers are very quick anyway, but you can follow this if you're very particular. But a lot of people consider it to be good practice to cut down on unnecessary stuff.
i * i <= n Here, i*i is i that is the iterating variable to the power/order 2.
We're doing this because we have assumed that factors of n cannot be more than the square root of n.
If i^2 = n
Then sqrt(n) = i
-> That is why we raise i to the power 2.
What this means is that instead of squaring i, you could also take sqrt(n) (which is the actual logic) in the parameter but that would require a math function which is unnecessary instead i*i is more simpler.
If it's too confusing then you can even just use for(int i = 3; i<n; i++), it will give you the same output.
When a function returns a bool value, you can put the function inside a conditional.
So writing if(is_prime(3) would return true and you can execute whatever is inside the conditional block like cout<<"3 is prime";
Over here bool is the preferred return type because we're just checking whether the number is prime or not. We're not returning any value like say the factors of that number.