C Program to Print Right Pascal Triangle Star Pattern

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

#include<stdio.h>
void loopcode(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++)
    {
        loopcode(rows, i);
    }
    for (i = rows; i >= 0; i--)
    {
        loopcode(rows, i);
    }
}
C Program to Print Right Pascal Triangle Star Pattern

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

In this Right Pascal Triangle Star Pattern program, we replaced the the C language for loop with the C programming while loop.

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

Output

Enter Rows to Print Right Pascal Triangle =  11

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

In the below Right Pascal Triangle pattern of Stars program, the user-defined function accepts the rows and characters to print the pattern using the symbol. For more star pattern examples, please refer to the C Star Pattern Programs article.

#include<stdio.h>
void loopcode(int rows, int i, char ch)
{
    for (int j = 0; j < i; j++)
    {
        printf("%c ",ch);
    }
    printf("\n");
}
int main(void)  {
    int i, rows;
    char ch;
    
    printf("Enter Character = ");
    scanf("%c", &ch);
    
    printf("Enter Rows to Print Right Pascal Triangle =  ");
    scanf("%d", &rows);
    
    for (i = 0; i < rows; i++)
    {
        loopcode(rows, i, ch);
    }
    for (i = rows; i >= 0; i--)
    {
        loopcode(rows, i,  ch);
    }
}

Output. Refer to the C programs article.

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

$ 
$ $ 
$ $ $ 
$ $ $ $ 
$ $ $ $ $ 
$ $ $ $ $ $ 
$ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ 
$ $ $ $ $ $ 
$ $ $ $ $ 
$ $ $ $ 
$ $ $ 
$ $ 
$ 

Also Read