In this tutorial, we will discuss the concept of C Program to print Pascal Triangle
In this topic, we are going to learn how to write a program to print Pascal triangle pattern using number in C programming language
Here, we use for, while and do-while loops for printing pascal triangle
Program to print Pascal Triangle
Display pascal triangle in C using loops
understanding Pascal Triangle
C code display pascal triangle using for loop
In this program, the user is asked to enter number of rows and then it will display pascal triangle number pattern using for loop in C language
Program 1
#include
#include
int main()
{
int rows,i,j,space,count=1;
//variable declaration
printf("Enter the No 0f rows: ");
scanf("%d",&rows);
//reading the input for rows
for(i=0; i
When the above code is executed, it produces the following result
Program to print Pascal Triangle
C code display pascal triangle using while loop
In this program, the user is asked to enter number of rows and then it will display pascal triangle number pattern using while loop in C language
Program 2
#include
#include
int main()
{
int rows,i,j,space,count=1;
printf("Enter the row for pascal triangle: ");
scanf("%d",&rows);//Enter number of rows to print pascal triangle
i=0;
while(i
When the above code is executed, it produces the following result
Program to print Pascal Triangle
C code display pascal triangle using do-while loop
In this program, the user is asked to enter number of rows and then it will display pascal triangle number pattern using do-while loop in C language
Program 3
#include
#include
int main()
{
int rows,i,j,space,count=1;
//variable declaration
printf("Enter the row for pascal triangle: ");
scanf("%d",&rows);//Enter number of rows to print pascal triangle
i=0;
do{//outer do-while loop to print space for every line
space=1;
do{
printf(" ");//print space
space++;
}while( space<=rows-i);
j=0;
do{//inner do-while loop for print pascal triangle
if(j==0 || i==0)
count=1;
else
count=count*(i-j+1)/j;
printf("%4d",count);//print pascal triangle
j++;
}while( j<=i);
printf("\n");//move to next line
i++;
} while(i
When the above code is executed, it produces the following result