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 >= 1; i += inc){
for (int j = 0; j < i; j++ )
std::cout<<'*';
std::cout<<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.