Question: This is an assignment I had to do, it is running but as soon as the program tries to output this line 3 is a
This is an assignment I had to do, it is running but as soon as the program tries to output this line "3 is a prime number.", it doesn't work. Could someone please explain what errors I made and how to fix it? This is c++ if you didn't know already.
bool isPrime(int);
// ********************************************
int main()
{
// Declare program variables.
char goAgain;
int numberToTest;
bool numberIsPrime{};
// Loop as many times as the user would like
do {
// Prompt the user to enter a number and read their answer
do {
cout << "What positive whole number would you like to test to see if it is prime: ";
cin >> numberToTest;
} while (numberToTest <= 0);// the number to test must be greater than 0
// TODO: Call function to determine if the user's number is a prime
// Pass one argument representing the number to be tested
// Assign the returned value to the correct variable
// numberIsPrime = isPrime(numberToTest);
// Display output
if (numberIsPrime)
cout << numberToTest << " is a prime number." << endl;
else
cout << numberToTest << " is NOT a prime number." << endl;
cout << endl;
// Ask the user if they want to test another number and read their answer
cout << "Do you want to test another number (y/n): ";
cin >> goAgain;
} while (tolower(goAgain) == 'y');// Repeat until the user answers something other than Y or y.
cout << "End program - ";
return 0;
}
// Define/Implement function HERE
bool isPrime(int a)
{
int i, counter = 0;
for (i = 2; i < a, i++;)
{
if (a % i == 0)
counter++;
}
if (counter == 0)
return true;
else
return false;
}
/* Sample Output
What positive whole number would you like to test to see if it is prime: -10
What positive whole number would you like to test to see if it is prime: 0
What positive whole number would you like to test to see if it is prime: 3
3 is a prime number.
Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 4
4 is NOT a prime number.
Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 5
5 is a prime number.
Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 67
67 is a prime number.
Do you want to test another number (y/n): n
End program - Press any key to continue . . .
*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
