Prime Numbers Program

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace 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;
 }
For the "Invalid Input".

1
2
3
4
	if (num <= 1)
    {
        cout << "Invalid input." << endl;
    }


Just right above the while loop.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

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;
 }

 int main()
 {
     int x = 0;

     cout << "Enter a number: ";
     cin >> x;

     if (primeCheck(x) == 1)
     {
         cout << "It is a Prime Number!" << endl;
     }
     else
        cout << "Not a prime." << endl;
        
    return 0;
}
Last edited on
I have to write a bool function named isPrime. I can just change primeCheck to isPrime right?
It's just a name, I can call it turdCity and it'll do the same thing.
Lol! Ok but now my "Invalid input" isn't working correctly...?
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)
		{
			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)
		{
			return false; //Edited
		}
		i++;
	}

        return true;
 }
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."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

bool isPrime (int num)
{
	int i = 2;

	if (num <= 1)
    {
        cout << "Invalid input." << endl;
    }

	while(i <= num - 1)
	{
		if(num % i == 0)
		{
			return false;
		}
		i++;
	}

        return true;
 }

 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;
  return false;
 }

 // Do prime check here, set result = true if it is a prime, result = false if it isn't.

 return true; // 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;
}

That should help :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}
Last edited on
I like prime number assignment. It is ease to do, but hard to finish. Because you can heavily optimize prime number algorithm.
Just use a flag variable to check whether the number is prime or not.
Like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void prime()
{
	int n,flag=0;
	cout << "Enter an integer number -->\t";
	cin >> n;
	if(n<2)
	{
		cout << "\nInvalid operation" << endl;
	}
	else if(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();
}
Last edited on
See This Code Maybe Help u

#include <iostream>
using namespace std;

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";

return (0);
}
Topic archived. No new replies allowed.