Like nobody has seen this program before...ha
I have built a program that takes in an integer and lets the user
know if it is prime or not. I wrote a bool function to handle to the
calculation but could not get it to return anything but "true". When
I changed to bool to a regular int isPrime program and passed an
actual value, 0 or 1, is seemed to work. Is there something I am
missing when I am returning a Boolean value? I placed both program
below to see if I can get any input or pointers. I didn't have to
use bool for the class, but I prefer to.
Boolean Program:
bool isPrime(int);
int main()
{
int val;
//Get a number from the user to check
cout << "please enter a number" << endl;
cin >> val;
if (isPrime(val))
// Calls the function and sends val to function
cout << val << " is a prime\n";
else
cout << val << " is not a prime\n.";
return(0);
}
bool isPrime(int number)
{
for( int i=2; i < number; i++)
{
int leftOver=(number % i);
if (leftOver==0)
{
return true;
break;
}
}
return false;
}
int isPrime Program:
int isPrime(int);
int main()
{
int val;
//Get a number from the user to check
cout << "please enter a number" << endl;
cin >> val;
if (isPrime(val))
// Calls the function and sends val to function
cout << val << " is a prime\n";
else
cout << val << " is not a prime\n.";
return(0);
}
// isPrime function
int isPrime(int number)
{
int response;
for( int i = 2; i <= number; i++)
{
int leftOver = (number % i);
if (leftOver == 0)
{
response = 0;
return response;
break;
}
}
response = 1;
return response;
}