How can I remove the last comma from a loop in C++ in a simple way?

后端 未结 6 1338
梦毁少年i
梦毁少年i 2020-12-06 13:31

This program is for printing prime numbers till the input given and separating every prime number with a comma.

void main(){

    int N, counter=0, isPrime;
         


        
6条回答
  •  猫巷女王i
    2020-12-06 13:52

    Just decide from a pre condition:

    bool first = true;
    for(j=2;j<=N;j++){
       // ...
       if(k==N) {
       if(!first) {
           cout << ',';
       }
       else {
           first = false;
       }
       cout<

提交回复
热议问题