Hello I got a task, and I don't have any idea how to do it. Maybe you can help me?
There is given a whole number (that you need to insert by yourself using the command cin>>), and I need to determine is that number a prime or not. If it's a prime number I need to cout >> "1" and if it's not cout "0" , also it's mandatory to use the function:
bool IsPrime(long long a)
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
char answer = 'y';
int prime;
bool isPrime(int prime);
while (answer == 'y' || answer == 'Y')
{
cout << "Please enter an integer and I will tell you if it is prime: " << endl;
cin >> prime;
if (prime <= 0)
{
cout << "Please make sure the number is above 0." << endl;
cin >> prime;
if (prime <=0)
{
cout << "The program will now terminate." << endl;
system("pause");
return 0;
}
}
if (isPrime(prime))
cout << prime << " is a prime number. " << endl;
else
cout << prime << " is not a prime number. " << endl;
system ("pause");
system("cls");
cout << "Would you like to try again? , enter (Y/y or N/n)" << endl;
cin >> answer;
}
return 0;
}
bool isPrime(int a)
{
for ( int i = 2; i <= a/2; i++)
{
if (a % i ==0)
return 0;
}
returntrue;
}