C Program to Print Left Pascal Triangle Star Pattern

Write a C program to print the Left Pascal Triangle Star Pattern using the while loop, for loop, and functions with an example. The below Left Pascal program uses the multiple nested for loops to iterate the rows and print the pattern of stars. As there is a code repetition, we have created a separate function with the logic.

#include<stdio.h>

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

int main(void)
{
    int i, rows;
    
    printf("Enter Rows to Print Left Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        loopLogic(rows, i);
    }
    for (i = rows - 1; i >= 1; i--) {
        loopLogic(rows, i);
    }
}
C Program to Print Left Pascal Triangle Star Pattern

C Program to Print Left Pascal Triangle Star Pattern using while loop

In this Left Pascal Triangle Star Pattern program, we replaced the for loop with the while loop.

#include<stdio.h>

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

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

Output

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

In the below Left Pascal Triangle pattern of Stars program, the user-defined function accepts the rows and characters to print the pattern using the symbol.

#include<stdio.h>

void loopLogic(int rows, int i, char ch)
{
    for (int j = i; j < rows; j++)
    {
        printf("  ");
    }
    for (int k = 1; k <= i; k++)
    {
        printf("%c ",ch);
    }
    printf("\n");
}

int main(void)
{
    int i, rows;
    char ch;
    
    printf("Enter Character = ");
    scanf("%c", &ch);
    
    printf("Enter Rows to Print Left Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 1; i <= rows; i++) {
        loopLogic(rows, i, ch);
    }
    for (i = rows - 1; i >= 1; i--) {
        loopLogic(rows, i, ch);
    }
}

Output

Enter Character = $
Enter Rows to Print Left Pascal Triangle =  15
                            $ 
                          $ $ 
                        $ $ $ 
                      $ $ $ $ 
                    $ $ $ $ $ 
                  $ $ $ $ $ $ 
                $ $ $ $ $ $ $ 
              $ $ $ $ $ $ $ $ 
            $ $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ $ 
        $ $ $ $ $ $ $ $ $ $ $ 
      $ $ $ $ $ $ $ $ $ $ $ $ 
    $ $ $ $ $ $ $ $ $ $ $ $ $ 
  $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
  $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
    $ $ $ $ $ $ $ $ $ $ $ $ $ 
      $ $ $ $ $ $ $ $ $ $ $ $ 
        $ $ $ $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ $ 
            $ $ $ $ $ $ $ $ $ 
              $ $ $ $ $ $ $ $ 
                $ $ $ $ $ $ $ 
                  $ $ $ $ $ $ 
                    $ $ $ $ $ 
                      $ $ $ $ 
                        $ $ $ 
                          $ $ 
                            $