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.
#include <string>
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
}
}
}
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.
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.