I have a program where the user enters a number and it prints all the prime numbers between 1 and that number. I have been able to do that. It must also print the max prime number, the greatest prime between 1 and that number. This is where I am having problems. I am kinda new at this.
The function getMaxPrime must call isPrime.
Any help would be appreciated.
Thanks!
#include <iostream>
using namespace std;
bool isPrime (int num)
{
int counter=0;
{
for(int i=1; i<=num ; i++)
if (num%i==0) counter++;
if (counter==2)
return true;
}
return false;
}
//prints primes
void printPrimes (int num)
{
for(int i=2;i<=num;i++)
if (isPrime(i)) cout<<i<<",";
}
// if there is no prime number between 1 and num, getMaxPrime() should return a 0.
int getMaxPrime (int num)
{
?????????????????
}
// main function
int main()
{
int number;
do
{
cout << endl << endl
<< "Please enter a number, 0 or negative to stop: ";
cin >> number;
if (number > 0)
{
cout << "All the prime numbers between 1 and "<< number <<" are: "<< endl;
printPrimes(number);
cout << "\nThe max prime is: ";
int maxPrime = getMaxPrime(number);
if (maxPrime == 0) cout << "none.";
else cout << maxPrime;
}
}
while (number>0);
return 0;
}