Deciding Prime number

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";



system("pause");
return 0;
}
I think there are a couple of issue with the logic you implemented, I would write something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace 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) return true;
  for(int i=2; i<num; i++){
    if (num%i==0) return false;
  }
  return true;
}


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++)
Last edited on
This is a little better:

1
2
3
4
5
6
7
8
9
bool is_prime(int n) {
    if (n < 2) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;    // 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)
            return false;
    return true;
}

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) return false; 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.



Last edited on
omg, thaank you, I got it. but what if we wanna run this program as long as the user put 0?
should i put these codes inside a while loop !=0 ? or....
If you meant: Run the program until the user inputs 0,

Enclose everything below int num; excluding system("pause") and return 0; within a while loop.

While loop's condition: while(num!=0) {}

And for this to work you need to initialize num. So change int num; to int num=1;

On a related note, system("pause"); is not advised by many people. You could instead replace it with cin.get() which is part of iostream itself.

Good luck!
Topic archived. No new replies allowed.