Question #23081

3. Write a program that will display the following pattern, given the value of n.
Example: if n = 4, output
*
**
***
****

Expert's answer

#include <iostream>

using namespace std;

int main()

{

unsigned size;

cout <<"Enter integer n>0 to get a pattern\n";

cin >>size;

int space=2*size-2;

cout <<"For n=" <<size <<":\n";

for ( int astr=1; astr<=size; ++astr )

{

for ( int j=0; j<space; ++j )

{

cout <<" ";

}

for ( int i=0;i<astr; ++i )

{

cout <<"*";

}

cout <<"\n";

space-=2;

}

return 0;

}

using namespace std;

int main()

{

unsigned size;

cout <<"Enter integer n>0 to get a pattern\n";

cin >>size;

int space=2*size-2;

cout <<"For n=" <<size <<":\n";

for ( int astr=1; astr<=size; ++astr )

{

for ( int j=0; j<space; ++j )

{

cout <<" ";

}

for ( int i=0;i<astr; ++i )

{

cout <<"*";

}

cout <<"\n";

space-=2;

}

return 0;

}

## Comments

## Leave a comment