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 is asked to enter the number of rows and then it will show a pascal triangle number pattern using for loop in the C language
Program 1
#include
#include
int main()
{
int arr[30],arr_Temp[30];
//declare two one dim arrays
int i,j,k,l, rowNo;
//declare variables
printf("Enter the number of rows: ");
scanf("%d",&rowNo);
arr_Temp[0]=1;
arr[0]=1;
//initialize array elements
for(j=0; j
When the above code is executed, it produces the following result
Enter the number of rows: 4
Output 1
C Code to display pascal triangle using while loop
In this program, the user is asked to enter the number of rows and then it will show a pascal triangle number pattern using the while loop in the C language
Program 2
#include
#include
int main()
{
int arr[30],arr_Temp[30];
//declare two one dim arrays
int i,j,k,l, num_Row;
//declare and initialize
printf("Enter the numer of rows: ");
scanf("%d",&num_Row);
arr_Temp[0]=1;
arr[0]=1;
//initialize array elements
j=0;
while(j
When the above code is executed, it produces the following result
Enter the number of rows:5
Output 3
C Code to display pascal triangle using do-while loop
In this program, the user is asked to enter the number of rows and then it will show a pascal triangle number pattern using the do-while loop in the C language
Program 3
#include
#include
int main()
{
int arr[30],arrTemp[30];
//declare two one dim arrays
int i,j,k,l, rowNo;
//declare and initialize
printf("Enter the value for the number of rows: ");
scanf("%d",&rowNo);
arrTemp[0]=1;
arr[0]=1;
//initialize array elements
j=0;
do{
printf(" ");
j++;
} while(j
When the above code is executed, it produces the following result