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

its used to indicate if the number is prime or not.

i know..but why then if delete that and i input 7, only 2 and 3 is the prime number..??
4 and 7 is also prime number but it is not in the output..

It resets the indicator for whether or not the number is prime for the next cycle through the loop. Otherwise it stays false and will keep breaking out of the inner for loop instead of testing.
The total number is simple just put a counter inside the if (isPrime) statement after cout << "Prime:";

>4 and 7 is also prime number but it is not in the output..

1. 4 Cannot be a prime number .

2. At line 34 , there is no need or resetting it to true .

3. At line 19 interate the "i" from 1 to n

I guess this should solve ur problem .

An improvised version of ur code

for (i=1;i<=n;i++)
   {
        prime=true;

        for (j=2;j<i;j++)
        {
            if (!(i%j))
            prime=false;
        }   
        if(prime)
        cout<<i<<endl;

    }

Replying to Jonsca:

Can you please give me some explanation using example..
such as input a value to be test..please..
i still not clear enough about the use of isPrime=true; at there..
Thanks..

i still not clear enough about the use of isPrime=true; at there..

Then move it up to the top as Rahul suggested.
In the original code the variable was initialized as true when it was declared. If in going through the loop, the number was not prime it is changed to false. If we leave it as false without resetting the next time it goes through the loop all the numbers will appear not be prime even if they are. It needs to be reset to the original value of true somehow so that we do not get false negatives.
The way Rahul did it makes sense also. He's essentially moving the initialization of the isPrime into the loop and resetting it at the top each time (which in a loop would be technically right after when it would have been reset at the bottom). He saves that earlier initialization step when the isPrime is declared but it doesn't make it any more efficient to do it that way (perhaps slightly more readable).
I think you are over thinking this. Sit down with a pencil and paper and go through the loop for your input of 7.

Oh..ok now i get the point..what you are saying..
We will have to reset the prime no. to true again, if not...if there is other no. that is false..it will not going into the loop..
thanks so much my dear jonsca..
you are very kind..thanks..

Also...Thank you my dear Rahul..

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.