我需要制作一个程序,将用户编号传递给 bool 函数,如果该数字是否为质数,该函数将返回bool
响应,然后如果该数字是否为质数,则让程序显示。这就是我所拥有的,我完全被困住了。
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
//Declare return function
bool isPrime(int);
//Declare constant
const int PRIME=2;
int main()
{
//Declare local variable to store user number
int user_num;
//Declare local variable to store result
bool result;
//Declare variable to hold return
bool status;
//Request number from user
cout << "Please enter a number" << endl;
cin >> user_num;
//Call function isPrime
bool isPrime(int user_num);
if (status==true)
cout << user_num << "is prime" << endl;
else
cout << user_num << "is not prime" << endl;
system ("PAUSE");
return 0;
} //end main
//Define bool function
bool isPrime(int number)
{
bool status;
double num_1=sqrt(number);
for (int i=2;i<=num_1;++i)
if(!(number%i))
status=false;
else
status=true;
return status;
}//End bool function