C Tutorial – A Star pyramid and String triangle using for loops

In this C language tutorial we will look at a much-requested subject, printing a star pyramid and a string triangle using for loops.

We know that this question is often asked as part of homework (lab) assignments, but we got so much requests that we couldn’t ignore it. BUT we are urging you to at least try to make the homework questions yourself! At the end of this tutorial we will give two alternative questions, so you can try to fix those yourself.

Star Pyramid in C

In this section we will look how to make a Star pyramid using three “for loops”. Take a look at the following source code example:


#include <stdio.h>

int main() {
        int rows, star, spaces;
        int number_of_stars = 6;
        int number_of_rows = number_of_stars;

        for (rows=1; rows <= number_of_rows; rows++) {
                for (spaces=1; spaces <= number_of_stars; spaces++) {
                        printf(" ");
                }
                for (star=1; star <= rows; star++) {
                        printf("*");
                        printf(" ");
                }
                printf("\n");
                number_of_stars = number_of_stars - 1;
        }
        return 0;
}

First we declare some variables, such as number_of_stars (the number of stars at the base of the pyramid) and number_of_rows (the height of the pyramid, in this case the same as the number_of_ stars). We first setup a ‘for loop’ (learn more about for loops here) so our pyramid is divided in rows.

In the second ‘for loop’ a certain number of spaces before a star. This way we can place a star at a certain point in each row. The actual placing of the star is done in the third ‘for loop’.

The third ‘for loop’ will print the number of stars that is needed for a certain row.

After we end a row with a newline escape sequence character we just have to lower the number_of_stars by one.

That is all there is to it! The output will look like this:


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

String Triangle in C

In the second source code example in this C language tutorial we will look at how to create a string triangle. Here is example:


#include <stdio.h>
#include <string why not check here.h>

int main() {

        int i,c,length;

        char arr[] = "Hello Everybody!";

        length = strlen(arr);

        for (i = length; i >= 0; i--) {
                printf("\n");
                for (c = 0; c < i; c++) {
                        printf("%c",arr[c]);
                }
        }
        return 0;
}

We start out by declaring some integers and an array of characters that is initialized with the string “Hello Everybody!”. After this is done, the length of the number of characters is determined and stored in the variable ‘length’.

This variable is then used in the first ‘for loop’. This ‘for loop’ loops until the last character is printed on the screen (by looping length times).

In the second ‘for loop’ the actual printing is done. In this loop the number of characters, determined by integer i, are printed. Because integer i is decreased in the first ‘for loop’, the number of characters printed will also decrease with each loop run.

The output will look like this:


Hello Everybody!
Hello Everybody
Hello Everybod
Hello Everybo
Hello Everyb
Hello Every
Hello Ever
Hello Eve
Hello Ev
Hello E
Hello
Hello
Hell
Hel
He
H

That is all there is to it.

Alternative Questions

Now that you’ve seen the two examples, it’s wise to make your own programs. Of course you can think up your own exercise, but we have also two for you: recreate the two previous source code example and make the result face the opposite position. So you get an upside down star pyramid and a string triangle with the point facing up.

The results of these exercise can be found here, but at least try them yourself first (that’s the only way to learn).

That’s that for this C tutorial, until the next C tutorial.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 30 responses to “C Tutorial – A Star pyramid and String triangle using for loops”

Why not let us know what you think by adding your own comment!

  1. of course not a doctor! on August 15th, 2012:

    thanks..,,,i really helped me…. in my assignment.

  2. include on September 1st, 2012:

    thanks a lot for this…it help me a lot

  3. Preeti on September 6th, 2012:

    thanks its a very nice example to learn c but i want some more programs………….

  4. admin on September 6th, 2012:

    @Preeti : also take a look at the C programming Algorithms for more program examples. And in the future there is more to come, so check back later!

  5. sudhir padalkar on September 9th, 2012:

    thanks… its helpful….

  6. zawadi on September 12th, 2012:

    thank u very much sir!!!!!you are good teacher for beginners of programming

  7. ANKIT on October 17th, 2012:

    its good trick to solve this problem

  8. swapnil pateriya on October 30th, 2012:

    thanks its a very nice example

  9. dfhyd' hdfh on November 7th, 2012:

    Nice

  10. Jagadesh on November 8th, 2012:

    Very Nice

  11. AndeiKa on November 8th, 2012:

    Thank you so much. You provided me with a better understanding of basic programming concepts.

  12. Mani on November 9th, 2012:

    nice

  13. Aakash Kumar Khatrik on November 17th, 2012:

    Sir i have done first task self….which is as below…
    #include
    #include
    int main(void)
    {

    int rows, star, spaces;
    int number_of_stars = 6;
    int number_of_rows = number_of_stars;

    for (rows=1; rows <= number_of_rows; rows++) {
    for (spaces=1; spaces = rows; star–) {
    printf(“*”);
    printf(” “);
    }
    printf(“\n”);
    number_of_stars = number_of_stars + 1;
    }

    getch();
    return 0;
    }

  14. GaRUi on November 19th, 2012:

    Just three for loops and three pyramids . Have Fun. GaRUi

    /* DH GaRUi */
    #include
    #include

    #define pyramid_floor 11
    #define pyramid_top 1
    #define pyramid_increase 2

    int main()
    {

    int i;

    for (i=pyramid_top; i<=pyramid_floor ; i+=pyramid_increase)
    {
    cout << setw(pyramid_floor-i/pyramid_increase)<< setfill(' ') << ' ' << setw(i) << setfill('*') << '*' << endl;
    }

    cout << endl;

    int j;

    for (i=pyramid_top, j=pyramid_floor/pyramid_increase; i<=pyramid_floor ; i+=pyramid_increase, –j)
    {
    if(i<pyramid_floor ) { cout << setw(j)<< setfill(' ') << ' ';}
    cout << setw(i) << setfill('*') << '*' << endl;
    }

    cout <=pyramid_top ; i-=pyramid_increase, ++j)
    {
    if(i<pyramid_floor ) { cout << setw(j)<< setfill(' ') << ' ';}
    cout << setw(i) << setfill('*') << '*' << endl;
    }

    return 0;
    }

  15. GaRUi on November 19th, 2012:

    #include iostream and iomanip line deleted on posting. I think this post mechanism use 0x3E and/or 0x3C ASCII codes for some redirection.

  16. jsureshreddy on December 19th, 2012:

    it is good you did good job.

  17. Manish on December 21st, 2012:

    Thanks,i had need of it

  18. k.laxmisindhuja on January 18th, 2013:

    thankyou this program is helpful to me

  19. doug86 on January 29th, 2013:

    the code for the pyramid is a bit confusing because you are subtracting the number of spaces each time you move to a next row but the variable is number_of_stars…thanks for the code you use a 1..2..3..4 pattern(with space after each star) and i learned the 1..3..5..7 pattern recently so at least i can know two ways of solving this problem you should do more pattern tutorials

  20. asim on March 23rd, 2013:

    as a beginner the diamond shape tutorial is a bit confusing

  21. dinesh on June 3rd, 2013:

    hai..!
    i want a program to print a given string in traingle(or pyramid) form by using only one for loop.

    can anyone do it for me??

  22. snoopy on September 9th, 2013:

    why do we lower the number of stars by 1 in the end (in the star pyramid)? please explain. thanx in advance:}

  23. rizwan on September 27th, 2013:

    hai,how to draw cirle in c,langauge can anybody help me>

  24. aashritha on October 1st, 2013:

    hai,how to print encoding and decoding programs in c( use 2 dimentional array)language

  25. aashritha on October 1st, 2013:

    #include
    #include

    int main() {

    int i,c,length;

    char arr[] = “Hello Everybody!”;

    length = strlen(arr);

    for (i = length; i >= 0; i–) {
    printf(“\n”);
    for (c = 0; c < i; c++) {
    printf("%c",arr[c]);
    }
    }
    return 0;
    }

  26. muhammad asim malik on October 3rd, 2013:

    sir how can i print a circle in c language…if anyone master here in c language then contact me at malik_02394@yahoo.com

  27. Aldi on October 20th, 2013:

    is it possible to make star pyramid with only 2 “for” ??

  28. Ravi on October 30th, 2013:

    Program for finding the alternate digits of the number If number is 35846 then Sum 1=3+8+6=17 Sum 2=5+4=9

  29. gc on December 21st, 2013:

    //check this out

    #include
    void main()
    {
    int i,j,s;

    printf(“\n”);
    for(i=0;ii;s–)
    {
    printf(” “);
    }
    for(j=0;j<=i;j++)
    {

    printf("1");
    }
    for(j=0;j<i;j++)
    {
    printf("1");
    }
    printf("\n");
    }
    for(i=0;i<=3;i++)
    {
    for(s=0;si;j–)
    {
    printf(“1”);
    }
    for(j=3;j>i;j–)
    {
    printf(“1”);
    }
    printf(“\n”);
    }
    return(0);
    }

  30. divyesh chhatbar on January 13th, 2014:

    #include
    #include
    void main()
    {
    int i,j,l=0;
    int k=1;
    clrscr();

    for (i=1;i=k;j–)
    {
    printf(“%d”,j);
    }
    k=k+i;
    }
    getch();