Write a program that displays n depth * pyramid.

For example,Input: 4

Output:

*

***

*****

*******

This is what i have so far but it not giving me what i want.

#include <iostream>

using namespace std;

int main()

{

int star;

int count=1;

cout<<"Enter the number: ";

cin>>star;

int length=(2*star)-1;

for(int i=1;i<=4;i++){

for(int j=1; j<=length; j++){

if(length/2==j){

for(int l=1; l<=count; l++){

cout<<"*";

}

count +=2;

j+=2;

}

}

cout<<endl;

}

}

For example,Input: 4

Output:

*

***

*****

*******

This is what i have so far but it not giving me what i want.

#include <iostream>

using namespace std;

int main()

{

int star;

int count=1;

cout<<"Enter the number: ";

cin>>star;

int length=(2*star)-1;

for(int i=1;i<=4;i++){

for(int j=1; j<=length; j++){

if(length/2==j){

for(int l=1; l<=count; l++){

cout<<"*";

}

count +=2;

j+=2;

}

}

cout<<endl;

}

}

Think about the question. So starting at zero, you need to print (2x + 1) stars (where 0 <= x <= length) for each row. So using this equation and writing down number of stars for each level and try using a simple nested-for loop to solve this.

Remember that since I started at 0, if you decide to start at one, the equation is now (2x - 1) for each row

Remember that since I started at 0, if you decide to start at one, the equation is now (2x - 1) for each row

Last edited on

Topic archived. No new replies allowed.