C Program to Print Triangle Star Pattern

Write a C program to print the Triangle Star Pattern using for loop, while loop, do while loop, and functions with an example. There are multiple types of triangles, and this blog post covers most of them.

C Program to Print Triangle Star Pattern using for loop

The below program uses the multiple nested for loops to iterate the triangle rows and columns. The first nested loop prints the empty spaces, and the below one prints the stars.

#include<stdio.h>
 
int main(void)
{
    int rows;
    printf("Enter Rows to Print Triangle =  ");
    scanf("%d", &rows);
    
    for (int i = 1 ; i <= rows; i++ )
    {
        for (int j = 0 ; j < rows - i; j++ )
        {
            printf("  ");
        }
        for (int k = 0 ; k < i * 2 - 1; k++ )
        {
            printf("* ");
        }
        printf("\n");
    }
}
C Program to Print Triangle Star Pattern

In this Triangle Pattern of Stars program, we replaced the for loop in the above example code with the while loop.

#include<stdio.h>
 
int main(void)
{
    int rows;
    printf("Enter Rows to Print Triangle =  ");
    scanf("%d", &rows);
    
    int i = 1 ;
    while (i <= rows )
    {
        int j = 0 ;
        while ( j < rows - i )
        {
            printf("  ");
            j++;
        }
        int k = 0 ;
        while ( k < i * 2 - 1 )
        {
            printf("* ");
            k++;
        }
        printf("\n");
        i++;
    }
}

Output

Enter Rows to Print Triangle =  10
                  * 
                * * * 
              * * * * * 
            * * * * * * * 
          * * * * * * * * * 
        * * * * * * * * * * * 
      * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * 

This example uses the do while loop and prints the Triangle Stars Pattern.

#include<stdio.h>
 
int main(void)
{
    int rows;
    printf("Enter Rows to Print Triangle =  ");
    scanf("%d", &rows);
    
    int i = 1 ;
    do
    {
        int j = 0 ;
        do
        {
            printf("  ");
            j++;
        }  while ( j <= rows - i );
        int k = 0 ;
        do
        {
            printf("* ");
            k++;
        }  while ( k < i * 2 - 1 );
        printf("\n");
        i++;
    } while (i <= rows );
}

Output

Enter Rows to Print Triangle =  13
                          * 
                        * * * 
                      * * * * * 
                    * * * * * * * 
                  * * * * * * * * * 
                * * * * * * * * * * * 
              * * * * * * * * * * * * * 
            * * * * * * * * * * * * * * * 
          * * * * * * * * * * * * * * * * * 
        * * * * * * * * * * * * * * * * * * * 
      * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * * * * * * * * * 

In the below Triangle Pattern of Stars program, we created a function that accepts the rows and any character and uses the symbol to print the pattern.

#include<stdio.h>

void loopLogic(int rows, char ch)
{
    for (int i = 1 ; i <= rows; i++ )
    {
        for (int j = 0 ; j < rows - i; j++ )
        {
            printf("  ");
        }
        for (int k = 0 ; k < i * 2 - 1; k++ )
        {
            printf("%c ", ch);
        }
        printf("\n");
    }
}
int main(void)
{
    int rows;
    char ch;
    
    printf("Enter Character = ");
    scanf("%c", &ch);
    
    printf("Enter Rows to Print Triangle =  ");
    scanf("%d", &rows);
    
    loopLogic(rows, ch);
}

Output

Enter Character = $
Enter Rows to Print Triangle =  16
                              $ 
                            $ $ $ 
                          $ $ $ $ $ 
                        $ $ $ $ $ $ $ 
                      $ $ $ $ $ $ $ $ $ 
                    $ $ $ $ $ $ $ $ $ $ $ 
                  $ $ $ $ $ $ $ $ $ $ $ $ $ 
                $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
              $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
            $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
        $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
      $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
  $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 

The below shown examples are different kinds of triangles of stars, and we use the for loop to display the patterns. However, you can use the Hyperlink to see the multiple approaches such as while loop, do while, and functions.

C Program to Print Hollow Triangle Star Pattern

For information on the Hollow Triangle Pattern of Stars >> Click Here.

#include<stdio.h>

int main(void)
{
    int i, j, k, rows;
    
    printf("Enter Hollow Triangle Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1 ; i <= rows; i++ )
    {
        for ( j = 1 ; j <= rows - i; j++ )
        {
            printf("  ");
        }
        for ( k = 1 ; k <= i * 2 - 1; k++ )
        {
            if (i == 1 || i == rows || k == 1 || k == i * 2 - 1 )
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output

Enter Hollow Triangle Rows =  11
                    * 
                  *   * 
                *       * 
              *           * 
            *               * 
          *                   * 
        *                       * 
      *                           * 
    *                               * 
  *                                   * 
* * * * * * * * * * * * * * * * * * * * * 

Inverted Triangle Star Pattern

For more information on the Inverted Triangle Pattern of Stars >> Click Here.

#include<stdio.h>
 
int main(void)
{
    int i, j, k, rows;
    printf("Enter Rows to Print Inverted Triangle =  ");
    scanf("%d", &rows);
    
    for (i = rows ; i > 0; i-- )
    {
        for (j = 1 ; j <= rows - i; j++ )
        {
            printf("  ");
        }
        for (k = 1 ; k <= i * 2 - 1; k++ )
        {
            printf("* ");
        }
        printf("\n");
    }
}

Output

Enter Rows to Print Inverted Triangle =  14
* * * * * * * * * * * * * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * * * * * * * * 
      * * * * * * * * * * * * * * * * * * * * * 
        * * * * * * * * * * * * * * * * * * * 
          * * * * * * * * * * * * * * * * * 
            * * * * * * * * * * * * * * * 
              * * * * * * * * * * * * * 
                * * * * * * * * * * * 
                  * * * * * * * * * 
                    * * * * * * * 
                      * * * * * 
                        * * * 
                          * 

Downward Triangle Star Pattern

For information on the Downward Triangle Pattern of Stars >> Click Here.

#include<stdio.h>
 
int main(void)
{
    int i, j, rows;
    printf("Enter Rows to Print Downward Triangle =  ");
    scanf("%d", &rows);
    
    for (i = rows - 1; i >= 0; i-- )
    {
        for (j = 0 ; j <= i; j++ )
        {
            printf("*  ");
        }
        printf("\n");
    }
}

Output

Enter Rows to Print Downward Triangle =  9
*  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  
*  *  *  *  *  *  
*  *  *  *  *  
*  *  *  *  
*  *  *  
*  *  
*  

Left Pascal Triangle Star Pattern

For information on the Left Pascal Triangle Pattern of Stars >> Click Here.

#include<stdio.h>

void loopCode(int rows, int i)
{
    int j, k;
    for (j = i; j < rows; j++)
    {
        printf("  ");
    }
    for (k = 1; k <= i; k++)
    {
        printf("* ");
    }
    printf("\n");
}

int main(void)
{
    int i, rows;
    
    printf("Enter Left Pascal Triangle Rows =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        loopCode(rows, i);
    }
    for (i = rows - 1; i >= 1; i--) {
        loopCode(rows, i);
    }
}

Output

Enter Left Pascal Triangle Rows =  8
              * 
            * * 
          * * * 
        * * * * 
      * * * * * 
    * * * * * * 
  * * * * * * * 
* * * * * * * * 
  * * * * * * * 
    * * * * * * 
      * * * * * 
        * * * * 
          * * * 
            * * 
              * 

Hollow Left Pascal Triangle Star Pattern

For more information on the Hollow Left Pascal Triangle Pattern of Stars >> Click Here.

#include<stdio.h>
void loopcode(int rows, int i)
{
    int j, k;
    for (j = i; j < rows; j++)
    {
        printf("  ");
    }
    for (k = 1; k <= i; k++)
    {
        if (k == 1 || k == i )
            printf("* ");
        else
            printf("  ");
    }
    printf("\n");
}
int main(void)  {
    int i, rows;
    
    printf("Enter Rows to Print Hollow Left Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++)
    {
        loopcode(rows, i);
    }
    for (i = rows - 1; i >= 1; i--)
    {
        loopcode(rows, i);
    }
}

Output

Enter Rows to Print Hollow Left Pascal Triangle =  11
                    * 
                  * * 
                *   * 
              *     * 
            *       * 
          *         * 
        *           * 
      *             * 
    *               * 
  *                 * 
*                   * 
  *                 * 
    *               * 
      *             * 
        *           * 
          *         * 
            *       * 
              *     * 
                *   * 
                  * * 
                    * 

C Program to Print Right Pascal Triangle Star Pattern

For more information on the Right Pascal Triangle Pattern of Stars >> Click Here.

#include<stdio.h>
void loopLogic(int rows, int i)
{
    for (int j = 0; j < i; j++)
    {
        printf("* ");
    }
    printf("\n");
}

int main(void)
{
    int i, rows;
    
    printf("Enter Rows to Print Right Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 0; i < rows; i++)
    {
        loopLogic(rows, i);
    }
    for (i = rows; i >= 0; i--)
    {
        loopLogic(rows, i);
    }
}

Output

Enter Rows to Print Right Pascal Triangle =  10

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

Hollow Right Pascal Triangle Star Pattern

For more information on the Hollow Right Pascal Triangle Star Pattern >> Click Here.

#include<stdio.h>

void loopLogic(int rows, int i)
{
    for (int j = 1; j <= i; j++)
    {
        if (j == 1 || j == i )
            printf("* ");
        else
            printf("  ");
    }
    printf("\n");
}

int main(void)
{
    int i, rows;
    
    printf("Enter Hollow Right Pascal Triangle Rows =  ");
    scanf("%d", &rows);
    
    for (i = 0; i < rows; i++)
    {
        loopLogic(rows, i);
    }
    for (i = rows; i >= 0; i--)
    {
        loopLogic(rows, i);
    }
}

Output

Enter Hollow Right Pascal Triangle Rows =  12

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

Right Angled Triangle Star Pattern

For more information on the Right Angled Triangle Star Pattern >> Click Here.

#include<stdio.h>

int main(void)  
{
    int rows;
    printf("Enter Rows to Print Right Angled Triangle =  ");
    scanf("%d", &rows);
    
    for (int i = 1; i <= rows; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("* ");
        }
        printf("\n");
    }
}

Output

Enter Rows to Print Right Angled Triangle =  15
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * * 
* * * * * * * * * * * * * * 
* * * * * * * * * * * * * * *