I have been receiving lot of emails and requests through the forums and inline forums attached to the tutorials on my website C and C++ Programming Resources about how to print pyramids and diamonds in different formats. So I have written this article to demonstrate how you can print pyramids and diamonds using for loop and if condition. Building a pyramid in c programming is quite easy, but you must have the understanding of how for loop works.

Need the Source Code only – Click Here

Most people ask to print the diamond of the form below. but with slight modifications you can print any shapes you like and any thing you want. For example in the source I have commented few lines which print the numbers instead of the * to construct the diamons shape with numbers. Below is diamond which is actually constructed using two pyramids, one of them is upside down. Figure 2 explains it in more detail.


Figure 1: Actual diamond

 width= width=

Figure 2: Two triangles making a complete diamond

To construct a diamond in C language we will need to make use of loop statements (for loop in our case) and conditional statement (for examples if statement).

So let’s assume that a user want to make a diamond of the radius 4, and we have stored that value in our variable n. So using ca language function scanf(); we will get this value from the user and store it in n.

As explained earlier that a diamond consists of two triangles or pyramids. So I am using a for statement which will be execute twice and on the basis of the counter used in for loop I will take a decision of which pyramid (triangle) I will print.

So now the actual code starts to build a diamond. First loop will count through the number user entered and will print the spaces equal to the number (n) which was entered by the user.

So now we are going to print the * to make one line of the pyramid (triangle). First line will print only 1 * as the value of (i) is 1 in the beginning and then increases 1 by 1.

So this block for (i=1;i<=n;i++){…} will print the first or upper pyramid (triangle) of the diamond.


To print the lower part of the diamond, its quite understandable that if we reverse this code then we can have the reversed triangle or upside down triangle to complete the whole diamond.

So the reverse loops to print fewer spaces in the beginning of the loop and more spaces towards the end of the loop.

And the second loop to print more * in the beginning and less * towards the end of the loop.

And print the new line character every time the loops counter increments.

I hope you can understand the code quite easily. I am going to provide full documented source code here so that you can copy and paste the code or you can download the code as a text file or C language file by clicking here.