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

Something like this.

// if there is no prime number between 1 and num, getMaxPrime() should return a 0.

int getMaxPrime (int num)
{
       int maxPrime = 0 ;
       for ( int i = 2 ; i <num ; i++ ) // Since between 1 and num means those numbers are not included
       {
               if ( isPrime( i ) )
               {
                       maxPrime = i ;
                }
        }
        return maxPrime;
}

That code is slow; instead start at the top and count downwards.

Thanks a lot, I almost had this.

Something like this.

// if there is no prime number between 1 and num, getMaxPrime() should return a 0.

int getMaxPrime (int num)
{
       int maxPrime = 0 ;
       for ( int i = 2 ; i <num ; i++ ) // Since between 1 and num means those numbers are not included
       {
               if ( isPrime( i ) )
               {
                       maxPrime = i ;
                }
        }
        return maxPrime;
}

That code is slow; instead start at the top and count downwards.

yeah you are right. Didnt think that much, just wrote the code straight on. If you change the code to

for ( int i = num ; i >= (int)sqrt( num ) ; i-- )
...

the result will be much faster.

So a better implementation will be like this.

// if there is no prime number between 1 and num, getMaxPrime() should return a 0.

int getMaxPrime (int num)
{
       int maxPrime = 0 ;
       for ( int i = num - 1 ; i > 1 ; i-- ) // Since between 1 and num means those numbers are not included
       {
               if ( isPrime( i ) )
               {
                       return i;
                }
        }
}

No need for the sqrt part. I was thinking something else. :o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.