title: How to count how many times a loop has been executed? c++
body: <p>How to count how many times a loop has been executed my code doesn't work as I expected it,</p>
  
  <p>find the primes numbers before n number input by user and display them and count how many </p>
  
  <p>numbers are in total</p>
  
  <p>ex.
  number input lets say 7,</p>
  
  <p>and there are 3 numbers before 7</p>
  
  <p>so it displays 2,3,5,7 and there are 3 prime numbers before 7</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
  
   int n, i, k;
   int counter = 0;
   bool isprime;
  
  
   cout &lt;&lt; "Enter a positive integer n: "; 
  
   cin &gt;&gt; n;
  
   for(int k = 2; k &lt;= n; k++)
   {
          isprime = true;
  
          for(int i = 2; i &lt;= k - 1; i++)
              if(k%i == 0)
          {
                  isprime = false;
          }
  
          if(isprime)
          cout &lt;&lt; k &lt;&lt; "\t";}
  
  
  
  cout &lt;&lt; "\nThere are " &lt;&lt; counter &lt;&lt; " primes less than " &lt;&lt; n;
    return 0;
  }
  </code></pre>