determining if 2 values are positive prime values

this is my first c++ class.

both values must be positive, if its negative it will output an error and make them input 2 new values.

and after getting both values, we must determine if they are prime numbers, if they are not prime then we have to output an error saying both values must be prime numbers.

this is what I have so far and it doesn't work.


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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

cout << "Enter the values for a and b: ";

int a, b;

cin >> a;

cin >> b;

if ((a < 0) || (b < 0))

{

cout << "ERROR! Please re-enter positive values for a and b: ";

cin >> a;

cin >> b;

}

else

{

while (a <= b)

{

if (a % 2 == 0 && a % 3 == 0 && a % 5 == 0 && a % 7 == 0)

{

cout << "a and b must be prime numbers.";

cout << "Enter the values for a and b: ";

cin >> a;

cin >> b;

}

}

}

}



Last edited on
First, you should indent your code so it is easier to read.

What is the purpose of while (a <= b)?

There are a few things wrong right now. Your input checking code should loop until the user gets the input correct.

if (a%2 == 0 && a%3 == 0) says, "if a is divisible by two and a is divisible by 3." A number like six will pass while 4 will fail.

Testing divisibility by 2, 3, 5 and 7 will only work for fairly small numbers. You need to check at least every prime number less than or equal to the square root of the number your testing. Use a loop for this.
sorry its my first time using this site and its my first C++ class. I re-wrote my code again but it does not work.

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
44
45
46
47
48
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

bool isPrime(int num) 
{
	bool primeResult = true;
	for (int i = 2; i <= num / 2; i++) 
        {
		if (num % i == 0) 
               {

			primeResult = false;
			break;
	        }
	}
	return primeResult;
}

int main()
{
	cout << "Enter the values for p and q: ";
	int p, q;
	cin >> p;
	cin >> q;

	while ((p < 0) || (q < 0))
	{
		cout << "ERROR! Please re-enter positive values for p and q: ";
		cin >> p;
		cin >> q;
	}

	if ((p > 0) && (q > 0))
	{
		if (!isPrime(p) || !isPrime(q))
		{
			cout << "p and q must be prime numbers. Enter the values for p and q: "
			cin >> p;
			cin >> q;
		}
	}
}
Last edited on
That's a lot better. You're almost done. Your isPrime function isn't particularly efficient, but it should work for all positive inputs except 1. One is not prime but your function would return true.

Lines 38 and 39 don't do anything. These functions return a value which you need to use.

1
2
if (isPrime(p) && isPrime(q))
    //p and q are both prime. 

so you are saying to delete this line
 
if ((p > 0) && (q > 0))


and leave it like this:
1
2
3
4
5
6
7
if (!isPrime(p) || !isPrime(q))
{
	cout << "p and q must be prime numbers. Enter the values for p and q: "
	cin >> p;
	cin >> q;
}
	
Topic archived. No new replies allowed.