How do I add the bool function isPrime to this and also an output that says "Invalid Input" to a number less than 2? I already tried adding the "Invalid Input" but it shows up on every number I enter along with whether it is prime or not. I don't know what I'm doing wrong.
#include <iostream>
usingnamespace std;
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
int i = 2;
while(i <= num - 1)
{
if(num % i == 0)
{
cout << num << " is not a prime number." << endl;
break;
}
i++;
}
if(i == num)
cout << num << " is a prime number." << endl;
return 0;
}
Thank you! I was putting it at the end. Do you know how to change it to a bool function. Everytime I try to change it, I get an error. I'm not sure what I'm doing wrong.
bool primeCheck (int num)
{
cout << "You've passed the number [" << num << "] from your function call." << endl;
int i = 2;
while(i <= num - 1)
{
if(num % i == 0)
{
return !primeCheck; //Edited
}
i++;
}
if(i == num)
return primeCheck;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bool primeCheck (int num)
{
cout << "You've passed the number [" << num << "] from your function call." << endl;
int i = 2;
while(i <= num - 1)
{
if(num % i == 0)
{
returnfalse; //Edited
}
i++;
}
returntrue;
}
Ok but what about my "Invalid input"? Like if someone were to enter the number 1 or anything less, how do I get it to say "Invalid input"? I did this below but when I enter a number 1 or less, it says both "Invalid input" and "1 is a prime number."
#include <iostream>
usingnamespace std;
bool isPrime (int num)
{
int i = 2;
if (num <= 1)
{
cout << "Invalid input." << endl;
}
while(i <= num - 1)
{
if(num % i == 0)
{
returnfalse;
}
i++;
}
returntrue;
}
int main()
{
int x = 0;
cout << "Enter a number: ";
cin >> x;
if (isPrime(x) == 1)
{
cout << x << " is a prime number." << endl;
}
else
cout << x << " is not a prime number." << endl;
return 0;
}
A method with a boolean value can only return true or false, if you want to return something else (e.g an error) then you can either throw and catch an exception or use a reference parameter.
1 2 3 4 5 6 7 8 9 10 11 12
// Checks if prime number, sets result to true if is, false otherwise
// Returns true on success, false on failure
bool IsPrime(int num, bool& result) {
if (num <= 1) {
cout << num << " is less than or equal to 1 and this is invalid" << endl;
returnfalse;
}
// Do prime check here, set result = true if it is a prime, result = false if it isn't.
returntrue; // nothing went wrong
}
Good convention is to order your parameters as inputs before outputs too, that is why result is after num. The & operator means we're passing in a boolean by reference so it can be changed in the method. You would call the method with:
1 2 3 4 5 6 7 8 9 10
bool is_prime;
bool success = isPrime(x, is_prime);
if (!success) {
exit(1);
} else {
if (is_prime)
cout << x << " is a prime" << endl;
else
cout << x << " is not a prime" << endl;
}
int main()
{
int x = 0;
cout << "Enter a number: ";
cin >> x;
if (x < 2)
cout << "Invalid input: " << x << endl;
if (isPrime(x))
{
cout << x << " is a prime number." << endl;
}
else
cout << x << " is not a prime number." << endl;
return 0;
}
void prime()
{
int n,flag=0;
cout << "Enter an integer number -->\t";
cin >> n;
if(n<2)
{
cout << "\nInvalid operation" << endl;
}
elseif(n==2)
{
cout << "\n\nThe number " << n << " is a prime number" << endl;
}
else
{
for(int i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
cout << "\n\nThe number " << n << " is a prime number" << endl;
else
cout << "\n\nThe number " << n << " is not a prime number" << endl;
}
_getch();
}
int main()
{
int n,p;
bool TEST; // becomes false if a number is equal to multiplication of two other numbers
TEST = true; // initialize TEST
cout<<"This is a prime number testing program \n" ;
cout<<"please input a number to test: ";
cin>> n;
for(int j=2 ;j<n; j++) // multiply all numbers below n to see if it gives n
{
for(int k=2 ;k<n; k++)
{
p=j*k;
if (p==n) // if a certain product equals n, TEST changes flag & loop is aborted
{
TEST = false;
break;
}
}
}
if (TEST == false)
cout << n << " is not prime";
else
cout << n << " is prime";