title: How can I remove the last comma from a loop in C++ in a simple way?
body: <p>This program is for printing prime numbers till the input given and separating every prime number with a comma.</p>
  
  <pre><code>void main(){
  
      int N, counter=0, isPrime;
  
      int k, j;
  
      cout &lt;&lt; "Enter maximum range: ";
  
      cin &gt;&gt; N;
  
      for (j=2; j&lt;=N; j++){
  
          isPrime = 0;
          k = 2;
  
          while (k&lt;j){
  
              if (j%k==0){
  
                  isPrime++;
              }
              k++;
          }
          if (isPrime==0){
  
              if (k==N){
                  cout &lt;&lt; j;
              }
              else{
                  cout &lt;&lt; j &lt;&lt; ",";
              }
              counter++;
          }
      }
      cout &lt;&lt; endl;
      system("pause");
  }
  </code></pre>
  
  <p>It is only removing the last comma for prime number inputs, not for any other input. How can I fix this?</p>
  
  <pre><code>Input: 23
  Output: 2,3,5,7,11,13,17,19,23
  Input: 8
  Output: 2,3,5,7,
  Input: 9
  Output: 2,3,5,7,
  </code></pre>