cpp program of pyramid pattern

In this example, how to print the pyramid pattern in the cpp programming language. Following is the code of the problem and the dry run description is in the last.


Code of pyramid pattern:

 #include<iostream>

using namespace std;

int main()

{

int num;

cout<<" enter the length of  pyramid "<<endl;

cin>>num;

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

{

for(int k=i;k>0;k--)

{

cout<<" ";

}

for(int j=i;j<num;j++)

{

cout<<"* ";

}

for(int k=num;k>0;k--)

{

cout<<" ";

}

cout<<endl;

}

return 0;

}


pyramid-in-cpp


In the above example, divide the problem into three portions, one is the print space, the second is the asterisk, and the third is the again spaces.

The first loop is used to printing the spaces which is terminate in half of the pyramid length, the second loop is used to printing the asterisk, and the third one is for again printing the spaces. In this way, the problem is solved by divide and conquer.

Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

Diferrence between flutter and react native