title: C++ program printing one more prime value that it should
body: <p>Here I have a C++ program that prints 2 to n prime numbers. The problem is, it is printing one digit more than what it needs to. Ex: n = 20 and it prints up to 23 instead of stopping at 19. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main()
  {
      int n, status = 1, num = 3, count, c;
      // User input to get n
      cout &lt;&lt; "Enter n: ";
      cin &gt;&gt; n;
  
      // Input is a possible prime number
      if (n &gt;= 1){
          // Print 2 as the first prime number
          cout &lt;&lt; 2 &lt;&lt; endl;
      }
      // loop that will iterate through n numbers
      for (count = 2 ; count &lt;=n ;){
          // for each element check whether it is prime or not
          for (c = 2 ; c &lt;= (int)sqrt(num) ; c++){
              // Not a prime number if divisble by something other than 1 or itself
              if (num%c == 0){
                  status = 0;
                  break;
              }
          }
          // Print prime numbers
          if (status != 0){
              cout &lt;&lt; num &lt;&lt; endl;
              count++;
              // If printed number is greater than n, stop the loop and printing
              if (num &gt;= n){
                  status = 0;
                  break;
              }
          }
          status = 1;
          num++;
      }         
  
      return 0;
  }
  </code></pre>