gihansalith gihansalith - 28 days ago 6
C++ Question

how to print char in multiple times in c++

2*

4**

6***

need to output above pattern
the code given below I have tried

#include <iostream>
#include<string>

using namespace std;

int main (){
string star = "*";
int a=2;
while(a<=6){
cout<<a<<star*(a/2)<<endl;
a+=2;
}
return 0;
}

Answer Source

The easiest way is probably

#include <iostream>
#include<string>

using namespace std;

int main (){
   int a=2;
   while(a<=6){
       cout<< a << std::string((a/2),'*') <<endl;
                // ^^^^^^^^^^^^^^^^^^^^^^
       a+=2;
   }
   return 0;
}