Printing stars using nested loops I just got back from my interview where I had to solve the following problem:


  Write a C++ program that prints the following pattern using nested for loops.


*
***
*****
***
*


Here is my solution:

int inc = 2;
for (int i = 1; i &gt;= 1; i += inc){
    for (int j = 0; j &lt; i; j++ )
        std::cout&lt;&lt;'*';
    std::cout&lt;&lt;endl;
    if ( i == 5 )
        inc = -2;
}


I'm just curious to know if there are better solutions (any criteria for better) or just different approaches.
Edit Oh well, not using nested for... Sorry about that

int inc = 2;
for (int i = 1; i &gt;= 1; i += inc){
    std::cout&lt;&lt;std::string(i,'*')&lt;&lt;endl;
    if ( i == 5 )
        inc = -2;
}