prime number

Pages: 12
I'm trying to make a program where user type a number and my program finds out if its prime or not and tells the user the result, but when i input the number 2 it says its not prime and i know why but i want to know how i can make it say 2 is prime.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main() {
	cout << "Enter a number to check if it's prime or not" << endl;
	
	int j = 2;
	int prime;
	cin >> prime;
	
	
		if(prime%j)
		{
			cout << prime << " is prime";
		}
		
		if(prime%j == 0) 
		{
			cout << prime << " is not prime";
		}	
		
	++j;
	return 0;
}
Last edited on
Initialize the variable 'j' to 3 and have a test condition that checks for 0, 1 and 2 then responds accordingly.
and have a test condition that checks for 0, 1 and 2 then responds accordingly.
what do you mean? can you give an example?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
switch(prime)
{
      case 0:
            cout << "Neither\n";
            break;
      case 1:
            cout << "Prime\n";
            break;
      case 2:
            cout << "Prime\n";
            break;
}
Last edited on
Now it says 6 is prime and 3 isn't prime, dont know what happend, would appreciate more help
Last edited on
Re-post your revised code. Maybe we can tell you.
okay what i have so far is:

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
39
#include <iostream>
using namespace std;

int main() 
{
	cout << "Enter a number to check if it's prime or not" << endl;
	
	int j = 3;
	int prime;
	cin >> prime;
	
	
		if(prime%j)
		{
			cout << prime << " is prime";
		}
		
		if(prime%j == 0) 
		{
			cout << prime << " is not prime";
		}	
		
		switch(prime)
		{
      		case 0:
            cout << "Neither\n";
            break;
      		case 1:
            cout << "Prime\n";
            break;
      		case 2:
            cout << "Prime\n";
            break;
		}
	++j;
	return 0;
}

it will say 2 is prime but will also say 3 isn't prime and if i make it say 2 AND 3 is prime 6 will be prime for some reason : /
Last edited on
This one is my fault OP. I misread your code. 'j' should be a 2 like you had it. But the switch I gave you doesn't go there. It would actually be a bit frustrating to plug it in anywhere as it is, I was just showing you an example of what I meant.
This looks like it should be looping, but I don't see any loop in there. You're currently only checking divisibility by 3, which is definitely not going to prove the primeness of a number.

Also, 1 is not a prime number.
okay :s
This seem to work fine

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
39
40
41
42
43
#include <iostream>
using namespace std;

int main() 
{
	cout << "Enter a number to check if it's prime or not" << endl;
	
	int j = 2;
	int prime;
	cin >> prime;

		switch(prime)
		{
      		case 2:
            cout << prime << " is Prime\n";
            goto LOOP;
            break;
            case 3:
            cout << prime << " is Prime\n";
            goto LOOP;
            break;
            case 9:
            cout << prime << " is not Prime\n";
            goto LOOP;
            break;
		}
	
	
	
		if(prime%j)
		{
			cout << prime << " is prime";
		}
		
		if(prime%j == 0) 
		{
			cout << prime << " is not prime";
		}	
		
	j++;
	LOOP:return 0;
}
Seem. 21?

goto is not a loop. You don't want to use goto.

Loops, see: http://www.cplusplus.com/doc/tutorial/control/
only tested 2 - 14 , hehe
Please use a for loop for something like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
            goto LOOP;
            break;
		}
	
	
	
		if(prime%j)
		{
			cout << prime << " is prime";
		}
		
		if(prime%j == 0) 
		{
			cout << prime << " is not prime";
		}	
		
	j++;
	LOOP:



Though I would personally use a prime sieve and if you really don't want to then do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(prime == 2) std::cout << "prime" << std::endl;
else if(prime > 2) //anything above 2(possible prime)
{
    bool isPrime = prime & 1; //if its odd then isPrime is true else false
    for(int i = 3; isPrime && i * i <= prime; i += 2) //iterate until the sqrt
    {
        isPrime = prime % i; //if it's not divisible it's possibly a prime
    }
    std::cout << (prime ? "prime." : "not prime.") << std::endl;
}
else //anything below 2 (not prime)
{
     std::cout << "not prime." << std::endl;
}


full sample:

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
// Example program - finding if a number is prime (w/o sieve which would be best)
#include <iostream>

int main()
{
    int prime = 0;
    while(std::cout << "Please enter a number to check if it is prime(0 to exit): " && std::cin >> prime && prime)
    {
        std::cout << prime << " is ";
        if(prime == 2)
        {
            std::cout << "prime." << std::endl;
        }
        else if(prime > 2) //anything above 2(possible prime)
        {
            bool isPrime = prime & 1; //if its odd then isPrime is true else false
            for(int i = 3; isPrime && i * i <= prime; i += 2) //iterate until the sqrt
            {
                isPrime = prime % i; //if it's not divisible it's possibly a prime
            }
            
            std::cout << (isPrime ? "prime." : "not prime.") << std::endl;
        }
        else //anything below 2 (not prime)
        {
            std::cout << "not prime." << std::endl;
        }
    }
    return 0;
}
Please enter a number to check if it is prime(0 to exit): 1
1 is not prime.
Please enter a number to check if it is prime(0 to exit): 2
2 is prime.
Please enter a number to check if it is prime(0 to exit): 3
3 is prime.
Please enter a number to check if it is prime(0 to exit): 4
4 is not prime.
Please enter a number to check if it is prime(0 to exit): 5
5 is prime.
Please enter a number to check if it is prime(0 to exit): 6
6 is not prime.
Please enter a number to check if it is prime(0 to exit): 7
7 is prime.
Please enter a number to check if it is prime(0 to exit): 8
8 is not prime.
Please enter a number to check if it is prime(0 to exit): 9
9 is not prime.
Please enter a number to check if it is prime(0 to exit): 10
10 is not prime.
Please enter a number to check if it is prime(0 to exit): 11
11 is prime.
Please enter a number to check if it is prime(0 to exit): 13
13 is prime.
Please enter a number to check if it is prime(0 to exit): 15
15 is not prime.
Please enter a number to check if it is prime(0 to exit): 17
17 is prime.
Please enter a number to check if it is prime(0 to exit): 19
19 is prime.
Please enter a number to check if it is prime(0 to exit): 23
23 is prime.
Please enter a number to check if it is prime(0 to exit): 0
yes but i dont learn much from that since i havent learnt alot of it in that code but thanks ;)
Which parts? The while loop? The for loop? The bitwise &? The ternary expression?
Didn't understand most of this and the while loop, havent worked with one like that before, hehe

1
2
3
4
5
6
7
8
9
10
else if(prime > 2) //anything above 2(possible prime)
        {
            bool isPrime = prime & 1; //if its odd then isPrime is true else false
            for(int i = 3; isPrime && i * i <= prime; i += 2) //iterate until the sqrt
            {
                isPrime = prime % i; //if it's not divisible it's possibly a prime
            }
            
            std::cout << (isPrime ? "prime." : "not prime.") << std::endl;
        }


i know what bool is and loops but the expressions/conditions seems really complicated for me.
Last edited on
Well lets look at it line by line.

line 1) we check if the number is greater than 2, if so it could possibly be a prime. If it's less than it won't be since there are none (besides 2 that we checked earlier).

line 3) this pretty much says if the number is even the it is not possibly a prime. You could write it like this bool isPrime = prime % 2; It pretty much checks if the first bit is set which would be the 1 bit.

line 4) it sets i equal to 3 since we already checked if it was divisible by 2. We know its not even so there is no need to check if it's divisible by any even numbers that is why we increment by 2 instead of 1 at the end of the condition you could also write it as i = i + 2 the center condition means if it could possibly be a prime then to search. The next part of the condition checks if i is less than or equal to the sqrt of the number. If you think about it the sqrt of 100 is 10. So the largest number to check would be 10 since 10 x 10 == 100 you will have already checked the rest. You could have written this as sqrt(i) <= prime the reason we have to check until the sqrt is because what if there is a number like 121 where it is made by 2 primes multiplied (11 x 11).

line 6) we just assign if it is divisible or not, if it is divisible it is not prime, if it is not divisible it is possibly a prime.

line 9) this is a ternary operator it would be the same as something like
1
2
3
4
5
6
7
8
9
if(isPrime)
{
    std::cout << "prime.";
}
else
{
    std::cout << "nor prime.";
}
std::cout << std::endl;
you lost me at
for(int i = 3; isPrime && i * i <= prime; i += 2) //iterate until the sqrt
if isPrime is not even it is 1 or true and 1 && 3 = 1 , then at
1 * 3 < = prime
is where im lost


ok i think i understand it more now i will just have to look it through a couple times more
Last edited on
Pages: 12