[i]The problem:[i] use bool isPrime(int) for a looping program that gets a number between 1-29 from user, output will say if it is prime or not, and they continue.
[i]The solution should look like:[i]
Enter a number
3
3 is a prime number.
Enter another number
8
8 is not a prime number.
and so on.
There doesn't seem to be an escape, it looks like they should go through the entire range.
For grading we are to use global constants and modules. When I put the constants in they don't seem to work.
What I've got will only go as far as displaying the number.
Thank you for your help.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <stdbool.h>
usingnamespace std;
//function prototypes
bool isPrime(int numero);
//global constants
constint PRIME_A = 0;
constint PRIME_B = 2;
int main()
{
int uNum = 0;
int i = 0;
// int //needs random number generator?
cout<<"Test for Prime Numbers."<<endl;
cout<<"Enter a positive number."<<endl;
cin>>uNum;
for (i=2; i <= 2; i++)
{
while (i >= 2 && i <= 29)
{
cin>>uNum;
break;
}
}
isPrime(uNum);
cout<<"Enter another number."<<uNum <<endl;
cin.get();
cin.get();
return 0;
}
bool isPrime(int numero)
{
int i;
int numPnP;
// const int PRIME_A = 0;
// const int PRIME_B = 2;
if (i <= PRIME_B && i % PRIME_B == PRIME_A )
{
cin>>numPnP;
cout<<"This is not a prime number."<<numPnP<<endl;
returnfalse;
}
else
{
cin>>numPnP;
cout<<"This is a prime number."<<numPnP<<endl;
returntrue;
}
}
Although the question you were given is not quite clear to me, I think the structure of the program should be something like this:
Note there is no use of cin or cout inside the function isPrime()
#include <iostream>
#include <cstdlib>
#include <cmath>
usingnamespace std;
//function prototypes
bool isPrime(int numero);
int main()
{
int uNum = 0;
cout << "Test for Prime Numbers." << endl;
cout << "\nEnter a number: ";
cin >> uNum;
while (uNum > 0 && uNum < 30)
{
// Test whether or not the number is prime
// Here, call the function isPrime(uNum))
// and output the appropriate message
// depending upon the returned value.
cout << "This is / is not a prime number." << endl;
cout << "\nEnter a number: ";
cin >> uNum;
}
cout << "Done." << endl;
cin.get();
cin.get();
return 0;
}
bool isPrime(int numero)
{
// add appropriate code here to test whether or not
// the number is prime, and return true or false as appropriate
returntrue;
}