title: Program to find prime numbers prints out two
body: <p>I am fairly new to programming and I wanted to try to write a program to find the amount of prime numbers with in a range of numbers. When I run this program through the compiler, I dont get any errors, but when I try to actually run the program it says that there are only 2, which is incorrect. I think it should be about 168. If you could help point out my error I would appreciate it. Thanks in advance!  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  void primeFinder(void);
  
  int main(void)
  {
      printf("Prime numbers from 1 to 1000:\n\n");
      primeFinder();
  
      return 0;
  }
  
  
  void primeFinder(void)
  {
      int i;
      int j;
      int k;
      int n_primes = 0;
  
      //i is the number to be tested:
      for ( i = 2 ; i &lt;= 1000 ; i++ )
      {
          //i must be divided by j, that goes from 2 to i - 1 [(i - 2) divisions]:
          for ( j = 2, k = 0 ; j &lt;= sqrt(i) ; j++ )
          {
              //i is not prime, whatever is the value of j:
              if ( i % j == 0 )
              {
                  //If remainder is 0, there is no need to test that i anymore:
                  break;
              }
              else
              {
                  k++;
              }
  
          } //End of inner for
              //i is prime:
          if ( k == i - 2 )
          {
              printf("%d\t", i);
              n_primes++;
          }
      } //End of outer for
      printf("\n\nIt was found %d prime(s) in the inverval considered.\n", n_primes);
  }
  </code></pre>