Here, we use for, while, and do-while loops for printing pascal triangle
Print Pascal Triangle
Display the pascal triangle in Java using loops
Pascal Triangle in Java
C Code to display pascal triangle using for loop
In this program, the user declares and initializes integer variables, it will display a pascal triangle number pattern usingfor loop in the C language according to the rows
Program 1
#include
#include
int main()
{
int arr[50][50];
int i=0,j=0,num=0;
printf("Enter the number o rows: ");
scanf("%d",&num);
for(i=0; i
When the above code is executed, it produces the following result
Output 1
C Code to display pascal triangle using while loop
In this program, the user declares and initializes integer variables, it will display a pascal triangle number pattern using a while loop in the C language according to the rows
Program 1
#include
#include
int main()
{
int arr[50][50];
int i=0,j=0,num=0;
printf("Enter the number o rows: ");
scanf("%d",&num);
i=0;
while(i
When the above code is executed, it produces the following result
Output 2
C Code to display pascal triangle using do-while loop
In this program, the user declares and initializes integer variables, it will show a pascal triangle number pattern using a do-while loop in the C language according to the rows
Program 3
#include
#include
int main()
{
int arr[50][50];
int i=0,j=0,num=0;
printf("Enter the number o rows: ");
scanf("%d",&num);
i=0;
do{
j=0;
do{
printf(" ");
++j;
}while(j<=num-1-i);
j=0;
do{
if(j==0 || j==i)
arr[i][j]=1;
else
arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
printf("%d ", arr[i][j]);
++j;
}while(j<=i);
printf("\n");
i++;
}while(i
When the above code is executed, it produces the following result