- On
- By
- 0 Comment
- Categories: do-while, for loop, Loop, Number pattern, While loop
C code to Generate Pascal triangle using 1 D array using user input
C code to Generate Pascal triangle using 1 D array using user input
In this tutorial, we will discuss the concept of the C code to generate a pascal triangle using 1 D arrays with user input
In this topic, we are going to learn how to write a program to print Pascal triangle patterns using a single dim array in the C programming language
Here, we use for, while, and do-while loops for printing pascal triangle

Display the pascal triangle in Java using loops
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 <stdio.h>
#include <stdlib.h>
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<rowNo; j++)
printf(" ");
printf(" 1\n");
for(i=1; i<rowNo; i++){
for(j=0; j<i; j++)
printf(" ");
for(k=1; k<=rowNo; k++){
arr[k]=arr_Temp[k-1]+arr_Temp[k];
}
arr[i]=1;
for(l=0; l<=i; l++){
printf("%3d",arr[l]);
arr_Temp[l]=arr[l];
}
printf("\n");
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter the number of rows: 4
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 <stdio.h>
#include <stdlib.h>
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<num_Row){
printf(" ");
j++;
}
printf(" 1\n");
i=1;
while(i<num_Row){
j=0;
while(j<i){
printf(" ");
j++;
}
k=1;
while(k<=num_Row){
arr[k]=arr_Temp[k-1]+arr_Temp[k];
k++;
}
arr[i]=1;
l=0;
while(l<=i){
printf("%3d",arr[l]);
arr_Temp[l]=arr[l];
l++;
}
printf("\n");
i++;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter the number of rows:5
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 <stdio.h>
#include <stdlib.h>
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<rowNo);
printf(" 1\n");
i=1;
do{
for(j=0; j<i; j++)
printf(" ");
k=1;
while(k<=rowNo){
arr[k]=arrTemp[k-1]+arrTemp[k];
k++;
}
arr[i]=1;
l=0;
do{
printf("%3d",arr[l]);
arrTemp[l]=arr[l];
l++;
} while(l<=i);
printf("\n");
i++;
}while(i<rowNo);
getch();
return 0;
}
When the above code is executed, it produces the following result
“Enter the value for the number of rows:4
Suggested for you
The operator in the in C language
Data type and variable in Java language
The operator in the Java language
Similar post
Java program to print pascal triangle
C program to print pascal triangle
C++ program to print pascal triangle
Java program to print pascal triangle using an array
Java program to print pascal triangle using array with user input
C code to Alphabet triangle pattern using the do-while loop
C++ code to Alphabet triangle pattern using the do-while loop
Java code to Alphabet triangle pattern using the do-while loop
Alphabet pattern in C language
Alphabet triangle pattern in C language using while loop
Alphabet pattern in Java language
Alphabet triangle pattern in Java language using while loop
Alphabet pattern in C++ language
Program to display pascal triangle pattern in C language