Pyramid and Hourglass!!!

As I am writing about patterns from last few blogs, now we are pretty much familiar with how to draw any basic pattern consisting of either number or any special symbols. For many people it was a cakewalk and hence I thought of raising the bar a little by trying some complex patterns. So broadly today we will discuss 2 pyramids and 2 hourglass kind of patterns and by this we will wrap our patterns concept, but remember this is not the end, you have to continue practicing lots and lots of problems in patterns. So without beating around the bush, lets start with our first pyramid pattern:

In our first pyramid pattern we have to print i stars in each row, starting from 1 to n, the input which we will give. We need three loops here, one for the number of times, one for spaces and one for printing the stars. The main thing we have to keep in mind is, instead of printing just *, we will print “* ”.

So as I said, we should not use separate loop for inside spaces, just we should print all the stars with a space.

In our next pattern, we don’t have spaces between the stars, these spaces will be replaced with stars. So our pattern will now look like this:

Hence now in each row we have 1,3,5… and so on stars. We can easily get the pattern, as no of stars in each row is:


eg. for third row, no of stars is 2*(3–1)+1=5 stars. Here we are doing (row-1) because we are starting with the index 0. So when are talking about row one, i value is 0 that time.

The rest of the code for the pattern is same as the above code,output and idle code is shown below:

So now we can see why we started with the basics as now we are easily able to apply those logics in these complex problems. Now comes the daddy of these patterns, if you are able to do it, then I am satisfied that you have learnt something out of this. The HOURGLASS pattern!!!

So this is the output when input is given as 15. It is quite big and looks complex and so lot of people think they will not be able to solve it. But if you observe closely, we can see that we have already done the code for bottom pyramid. Also we know how to run the loop in reverse and print the exact opposite of any pattern as discussed in previous blogs. So now we have to just combine both our codes into a simpler and single code.

What I will do is I will divide my program into 2 parts, one for the above pyramid and one for the below. Above pyramid loop will run n times and the other one will run n-1 times (as the common single * will be already printed in the above pyramid, so we should not print it again in lower pyramid). So by using pre requisite knowledge of reverse loop and above pattern, we can get our code and output as:

So I hope you would have enjoyed coding these patterns as they were a bit tougher than all previous problems. I will give you a codework for today, the below shown pattern should be printed only if input is odd, if it is even, a message should be displayed saying that pattern is not possible. The pattern is:


Similar pattern should be printed for all the odd inputs, as shown in the left hand side. For even inputs, message should pop. Give it a try, it is pretty simple, try some more patterns and remember keep coding…