Can someone explain me this program based on nested loops?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
 
int main ()
{
   int i, j;
   
   for(i=2; i<100; i++) 
   {
      for(j=2; j <= (i/j); j++)
        if(!(i%j)) break; // if factor found, not prime
        if(j > (i/j)) cout << i << " is prime\n";
   }
   return 0;
}



I want to know the logic and how to flow of control processes the program.
Last edited on
This code is very poorly formatted.

First of all, please post your code using the code tags under the Format: area.

So, something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <iostream>
using namespace std;

int main ()
    {
    int
        i = 0,
        j = 0;    //    Get in the habit of initializing all variables!

    for( i = 2;  i < 100;  i++ ) 
        {
        for( j = 2;  j <= ( i / j );  j++ )
            {
            if( ! ( i % j ) )
                break;        // if factor found, not prime

            }    /*   The second "for" loop    */

        if( j > ( i / j ) )
            cout << i << " is prime\n";

        }    /*    The first "for" loop    */

    return 0;

    }
        /*    main()    */


This should be easier to read. Now if you can't figure it out, please come back and ask your questions again.
Topic archived. No new replies allowed.