# C Program to Print Pascal’s Triangle

Write a C Program to Print Pascal’s Triangle.

Example,

```Input
5
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1```

## Explanation

The first row of Pascal’s Triangle contain only one element that is 1.

The number of elements in any row is one more than the number of elements in previous row.

The Left-most and Right-most element of every row is same and is equal to 1.

The value of an element (except for Left-most and Right-most) in any row is given by the sum of 2 elements in the previous row which are just above this element.

See the following illustration of Pascal’s Triangle for 5 rows.

## C Program to Print Pascal’s Triangle using 2D Array

``````#include<stdio.h>

// function to print Pascal's Triangle
int PascalsTriangle(int n) {
int arr;
int i, j;

for (i = 0; i < n; ++i) {

// printing spaces
for (j = 0; j < n - 1 - i; ++j) {
printf(" ");
}

// calculating then printing the data of ith
// row of Pascal's Trianle
for (j = 0; j <= i; ++j) {
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]);
}

printf("\n");

}

}

int main() {

int n;
printf("Enter Number of Rows in Pascal's Trianle : ");
scanf("%d", &n);
PascalsTriangle(n);
return 0;

}``````

Output

## C Program to Print Pascal’s Triangle using Jagged Array

The problem in using a 2D array to construct Pascal’s Triangle is that we are not using all indices of the array. About half the array remains unused. Thus, instead of using a 2D array, we use a Jagged Array.

A Jagged Array is basically an array of arrays but the size of each array is different. We will use dynamic memory allocation to assign memory to the Jagged Array.

``````#include<stdio.h>
#include<stdlib.h>

// function to print Pascal's Triangle
int PascalsTriangle(int n) {
int** arr;
int i, j;

//
arr = (int**)malloc(sizeof(int*) * n);

for (i = 0; i < n; ++i) {

arr[i] = (int*)malloc(sizeof(int) * (i + 1));

// printing spaces
for (j = 0; j < n - 1 - i; ++j) {
printf(" ");
}

// calculating then printing the data of ith
// row of Pascal's Trianle
for (j = 0; j <= i; ++j) {
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]);
}

printf("\n");

}

}

int main() {

int n;
printf("Enter Number of Rows in Pascal's Trianle : ");
scanf("%d", &n);
PascalsTriangle(n);
return 0;

}``````

Output

## C Program to Print Pascal’s Triangle using 1D Array

``````#include<stdio.h>

// function to print Pascal's Triangle
int PascalsTriangle(int n) {
int arr;
int i, j;

for (i = 0; i < n; ++i) {

// printing spaces
for (j = 0; j < n - 1 - i; ++j) {
printf(" ");
}

// calculating then printing the data of ith
// row of Pascal's Trianle
for (j = i; j >= 0; --j) {

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

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

printf("\n");

}

}

int main() {

int n;
printf("Enter Number of Rows in Pascal's Trianle : ");
scanf("%d", &n);
PascalsTriangle(n);
return 0;

}``````

Output

## C Program to Print Pascal’s Triangle Using Recursion

``````#include<stdio.h>
#include<string.h>
#include<math.h>

// function to print Pascal's Triangle
// i means we are on (i+1)th row
// parr is the pascal's triangle values of previous list
void PascalsTriangle(int n, int i, int* arr) {

// if i>=n is true
// then it means we have printed all n rows
// of the pascal triangle
if (i >= n)
return;

int j;

// printing spaces
for (j = 0; j < n - 1 - i; ++j) {
printf(" ");
}

// calculating then printing the data of ith
// row of Pascal's Trianle
for (j = i; j >= 0; --j) {

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

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

printf("\n");

PascalsTriangle(n, i + 1, arr);

}

int main() {

int n;
int arr;

printf("Enter Number of Rows in Pascal's Trianle : ");
scanf("%d", &n);

arr = 1;
PascalsTriangle(n, 0, arr);

return 0;

}``````

Output

What to Study Next?

1. Diamond Pattern Program

References

Pascal Triangle