-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Thursday 30 April 2015

Making a Pyramid

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a new blog that will help you to make a pyramid in C++.

What is a pyramid?

The above figure is actually called a pyramid.
After reading this blog, you would be able to draw a pyramid very easily inshallah.So,below is the source code of this program.

Source code



Output


How the program works?


In this program I have used 3 loops. The “s” loop is actually used for spaces. So, according to the given condition, the value of loop “i” is from 0-10.It is actually the outer loop. As the condition is true, so it will move towards the next loop, “s”.

 The value of “s” loop is from i-10.It is used for printing spaces. In the first iteration, it will print 11 spaces(and in the next iteration,it will print 10 spaces). After printing spaces, it will move towards next loop that is used to print the stars.

The value of “j” loop is from 0 - i*2. It means that it will display only 1 star in the beginning.

Now, in the next iteration, it will display 3 stars as the value of j is from 0 - 1*2(note that the value of i is 1 in this condition).


This process continues again and again and as a result, a pyramid is displayed before the user.



No comments:

Post a Comment