title: Finding Twin Primes - Have logic finished, but it won't print anything when the program is ran
body: <p>A twin prime is a prime number that is exactly two larger than the largest prime number that is smaller than it. For example, 7 is a twin prime because it is exactly two larger than 5. But 17 is not a twin prime because the largest prime less than 17 is 13.</p>
  
  <p>My logic for this program is as follows:</p>
  
  <pre><code>*ask number of twin primes that want to be found
   *loop until desired number of twin primes are found
     *loop numbers 2 - 1million (declared as variable j)
      *check if that number 'j' is prime - if so flag it
       *if 'j' is not flagged, subtract 2 from 'j' (call that new number 'TPcheck')
        *Check if 'TPcheck' is a prime, if so, print 'TPcheck' and the first number 'j'
  </code></pre>
  
  <p>When I run this program, I enter the number of twin primes to be found, but it just continues to run, and doesn't print anything on the screen. I think that the problem may have something to do with the order of the loops and if statements(or maybe the way that they are nested), but I have tried a ton of different ways and nothing has worked.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
  
  int i = 2, count = 0, TPcheck, j, k, flag;
  int numberofTwinPrimes;
  printf("Enter how many twin primes you want to find");
  scanf("%d", &amp;numberofTwinPrimes);
  
  
  while(count &lt; numberofTwinPrimes)
  {
      for(j = 2; j &lt;= 1000000; ++j)
      {   for(i = 2; i &lt; j; ++i)
          {
              if(j%i == 0)
              {
                  flag = 1;                   
                  continue;
              }
  
              if(flag == 0)
              {
                  TPcheck = j - 2;
                  for(k = 2; k &lt; TPcheck; ++k)
                  {
                      if(TPcheck%k == 0)
                      {
                          flag = 1;
                          continue;
                      }
  
                      if(flag == 0)
                      {
                          printf("%d\t %d\t", TPcheck, j);
                          count++;                
                      }
                  }
              }           
          }
  
      }
  }                       
  
  
  return 0;
  
  }
  </code></pre>