How can I get the following program to print a string output

It prints 1 for TRUE
and 0 for FALSE

I want it to print "It is a prime number." if the output is a 1.
and "It is not a prime number. " if the output is a 0.

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
  //A recursive function to determine if an input is prime:
#include<iostream>
using namespace std;

bool isPrime(int p, int i);

int main() {
	
	int p, i = 2;
	cout<<"\nEnter a number: \n";
	cin>> p;
	cout << "Results: ";
	cout<<	  isPrime(p, i);
}
bool isPrime(int p, int i)
{
	if (i == p)
		 return 1;         //or better  if (i*i>p) return 1;
	
	if (p % i == 0)
	
		 return 0;
	
	else
			 return isPrime (p, i+1);
		
}
You obviously know how to use an if statement:

Replace line 13 with:
1
2
3
4
  if (isPrime(p,i))
    cout << "It is a prime number." << endl;
  else
    cout << ""It is not a prime number. " << endl; 

Last edited on
Thanks very much AbstractionAnon
Topic archived. No new replies allowed.