C and C++ Programming Resources

Custom Search

Print pyramids and diamonds in C Language

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.

 width=

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.

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 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.

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","*");   
//printf("%d",i);
}

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

 width=

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 by clicking here.

Tags: , ,

There are 9 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.

  • loveb says:

    I have creatred proramme for the same triangle using formated input output it give less iteration of loops so execution time nd run time is decreases about 45 percent for pls check out and if any mistake pls infrom me

    #include
    #include
    void main(){
    int i,j,no;
    printf(“Enter the no: “);
    scanf(“%d”,&no);
    for(i=1;i<=no;i++){
    printf("%*c\b",no-i+1,' ');
    for(j=1;j=1;i–){
    printf(“%*c\b”,no-i+1,’ ‘);
    for(j=1;j<=i;j++){
    printf(" *");
    getch();
    }
    printf("\n");
    }
    getch();

    }

  • nckeerthi says:

    please help me how can i get the output a follows give me a simple code in c pleaseeeeeeeee its urgent
    * *
    ** *
    * * *
    * * *
    * *
    guys please help me its important i should get it by tomarrow so please mail this code to my email id pleaseeeeeeee

  • nckeerthi says:

    oh noo its not like that
    it must be in n shape
    * *
    ** *
    * * *
    * * *
    * *

  • Raghavendra says:

    Hello all….
    i need o print stsrs in this format…

    *
    * * *
    * * * * *
    * *
    * * * *
    * * * * * *
    * * * *
    * *
    * * * * *
    * * *
    *

  • iti says:

    how can i get out put in following format??
    123454321
    1234 4321
    123 321
    12 21
    1 1
    12 21
    123 321
    1234 4321
    123454321

  • yeswanth says:

    hi everybody

    i need pyramid like this

    **********
    ***** ****
    **** ***
    ** **
    * *
    ** **
    *** ***
    ***** ****
    **********

    i need pyramid like this would anybody plz help me….

  • ramya says:

    I need the fellowing output

    enter the no: 8
    enter the symbol: A

    A
    AAA
    AAAAA
    AAAAAAA
    AAAAAAAAA

    please do the needful

  • Bs.Ram says:

    Hello… Ramya… try.. this..pls

    void main()
    {
    int i,j,n;
    cout<>n;
    for(i<0;i {
    for(j=0;j {
    cout<<” A”;
    }
    cout<<“\n”;
    }
    }

  • Bs.Ram says:

    void main()
    {
    int i,j,n;
    cout<>n;
    for(i<0;i {
    for(j=0;j {
    cout<<“A”;
    }
    cout<<“\n”;
    }
    }


Leave a Reply

You must be logged in to post a comment.