Hi....can anyone help me to explain what is the use of isPrime=true;
in the last line of this coding..??
I found that it is very important because if i delete it, the answer of the prime number will always constantly 2 and 3..
Even if i put 7 as the input ,the result will also became 2 and 3. The 4 and 7 is not there..Can anyone explain this ??
Then,,i also want to find the total element of the prime number...
For Example The output will became like this..
Enter a number and I will generate the prime numbers up to that number: 7
Prime : 2
Prime : 3
Prime : 5
Prime : 7
The total is :4
How is it?..
Please anyone..help me to get out from this frustrating..
Thanks so much..
#include <iostream>
void primeNum(int num);
using namespace std;
int main()
{
int n;
cout << " Enter a number and I will generate the prime numbers up to that number: ";
cin >> n;
primeNum(n);
}
void primeNum(int num)
{
bool isPrime=true;
for (int i = 2; i <= num; i++)
{
for ( int j = 2; j <i; j++)
{
if ( i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true; //WHAT it is mean??
}