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

i just found this site so i dono how to add numbers to the each line and make it look nice but is there anyone who can give me like step by step description of whats happening here?? i just need to study for the final that is coming up because prof. said there will be question on prime number

If you wrote it presumably you know what's going on, or what you want to have happen, and might not be. If you have a specific question post it. As it stands there is plenty of questions you could be asking. I would prefer not to step in and tackle them all.

First, to preserve the code syntax formatting you use when writing code please post code using code tags. The description of their use is found at the top of the board in the announcements section as well as in the sticky threads at the top of the board.

well it wasn't really me who made this code.... i found this code from one of the thread in this website and it looked similar to what my professor showed to me in the class for the review guide. so i thought might as well know the concept behind the code just in case my prof ask similar question in my final.

Put it back where you found it then and start writing your own code. I suspect you can do better than what you found. One basic protocol to finding out if a number is prime is as follows:

``````int number = //some value
bool isPrime = true;
for(int i = 2; i < number; ++i)
{
if(number % i == 0)
{
isPrime = false;
break;
}
}``````

This can be tuned up some to make it more efficient, but that's the basics. I presume if you've made it this far in your class, then you are familiar with how the modulo operatorn, %, works.

alright thx i guess i will play around with the code more to see what i can do

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.