I have been receiving a 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 using C program.
This article demonstrates how you can print pyramids and diamonds using for loop and if condition. Building a pyramid in c programming is quite easy provided that you understand the concept of how loops works.
If you want to download the Source Code of this article you can follow this link: Print Pyramids/Diamond Shares using C.
Generally it is requested to print the diamond of the form below, but with slight modifications you can print any shapes you like. For example in the source I have commented few lines which print the numbers instead of the * to construct the diamonds 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
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 a C function scanf(); we will get this value from the user and store it in n.
scanf("%d",&n);
As explained earlier that a diamond consists of two triangles or pyramids. So I am using a for statement which will be executed twice and on the basis of the counter I will take a decision of which pyramid (triangle) I will print.
for(m=0; m<2; m++)
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.
for (i=1; i<=n; i++){//loop to count lines in the triangles if(m==0){//just a decision of whether we are printing the first or second part of the diamond for(k = 1; k<=n-i; k++){//loop to print the actual spaces before * will be printed printf(" "); } } }
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.
for(k=1; k<2*i; k++){ printf("%s","*"); }
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.
for(k = 1; k <= i; k++)
And the second loop to print more * in the beginning and less * towards the end of the loop.
for(k=1; k < (n-i)*2; k++)
And print the new line character every time the loops counter increments.
for (i = 1; i <= n; i++){ //code to print pyramids printf("\n"); }
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.