Home » Pascal Triangle in C using 2d Array

# Pascal Triangle in C using 2d Array

Learn about Pascal Triangle in C using 2d Array in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

#### Pascal Triangle in C using 2d Array

To print a pascal triangle like below then follow the below C program.

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

Program:

``````#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 ;
}``````

Output:

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

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.