# Pascal Triangle Program in C

Pascal triangle is a triangular array of binomial coefficients. In pascal’s triangle, each number is the sum of the two numbers directly above it. Here we will write a pascal triangle program in the C programming language.

``````                                       1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 ``````

## Pascal Triangle Program in C Without Using Array

``````#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int row=1; row<=n; row++)
{
int a=1;
for(int i=1; i<=row; i++)
{
printf("%d ",a);
a = a * (row-i)/i;
}
printf("\n");
}
return 0;
}``````

Output for different test-cases:-

Enter the number of rows: 5
` 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 `

Enter the number of rows: 7
` 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1`

### At the center of the screen

We can display the pascal triangle at the center of the screen. For this, just add the spaces before displaying every row. Generally, on a computer screen, we can display a maximum of 80 characters horizontally. Half of 80 is 40, so 40th place is the center of the line.

``````#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int row=1; row<=n; row++)
{
int a=1;

for(int s=1; s<=40-row; s++)
printf(" ");

for(int i=1; i<=row; i++)
{
printf("%d ",a);
a = a * (row-i)/i;
}

printf("\n");
}

return 0;
}``````

Output for different test-cases:-

``````Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 ``````
``````Enter the number of rows: 7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1 ``````

## Pascal Triangle in C using 2d Array

``````#include<stdio.h>
int main(void)
{
int n, i, j ;

printf("Enter the number of rows: ");
scanf("%d",&n);

int pascal[n][n];
for(i=0;i<n;++i)
{

for(int s=1; s<=40-i; s++)
printf(" "); //space

for(int j=0;j<=i;++j)
{
if(j==0||j==i) pascal[i][j]=1;
else
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];

printf("%d ",pascal[i][j]);
}

printf("\n");
}

return 0 ;
}``````

In this program, we took a 2d array of size n, where n is the number of rows entered from the user.

The first inner loop displays the space on the output screen. if you don’t want to display the output at the center of the screen then remove this for a loop.

In the pascal triangle, in every row, the first and last number is 1 and the remaining are the sum of the two numbers directly above it. So, inside the second inner for loop, we write the below condition,

``````if(j==0||j==i) pascal[i][j]=1;
else pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];``````

Note that in every row the size of the array is n, but in 1st row, the only first element is filled and the remaining have garbage value. Similarly, in the second row, only the first and second elements of the array are filled and remaining to have garbage value. We don’t want to display the garbage value. So, it will be easy for us to display the output at the time of calculation.

Learn more:- The mathematical secrets of Pascal’s triangle

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!