Printing Pascal’s Triangle in C
Pascal’s Triangle
“Pascal's triangle is an essential concept of algebra and computer science. It is a triangular arrangement of numbers in which the first and last numbers are one, and the other numbers are the sum of two numbers just above it.”
It was named after the famous mathematician Blaise Pascal. The Pascal triangle is taught to engineering students as a genius engineering formula. It can be derived by using binomial expressions.
C Program to Print Pascal’s Triangle
The structure of Pascal's triangle is based on the structure that the first and last values will be one, and the rest values are the sum of the two values just above it.
Pascal's triangle can be derived using combination and factorial. Here we will first develop an algorithm for the approach.
Algorithm
- Take the input for the number of rows to be printed in Pascal's triangle as N.
- Make an iteration for N times to print the rows. Name this iteration I
- Make an inner iteration J (N-1) times.
- Print single whitespace.
- Close the inner loop.
- Make another inner loop J for I times.
- Print nCr of I and J, i.e., ICJ.
- Close the inner loop.
- Put a line break for each outer iteration.
- Exit.
Pseudocode
1. Input N, the number of rows in Pascal's triangle.
2. For row I in range(N):
a. For space J in range(N-1):
- Print a whitespace
b. For column J in range(I+1):
- Evaluate nCr for I and J.
- Print the calculated value.
c. Print a line break.
3. Exit.
C Code for Implementation of the Algorithm
Code:
#include<stdio.h> int factorial(int n){ int f; for(f =1; n >1; n--) f *= n; return f; } int ncr(int n,int r){ return factorial(n)/( factorial(n-r)* factorial(r)); } int main(){ int n, i, j; n =5; for(i =0; i <= n; i++){ for(j =0; j <= n-i; j++) printf(" "); for(j =0; j <= i; j++) printf(" %3d", ncr(i, j)); printf("\n"); } return0; }
Output:
We have printed Pascal's triangle using the binomial theorem. This is one way of printing Pascal's triangle. There are several other ways to do this. Let us see the code for other ways of printing Pascal's triangle.
Using 2D Arrays
It can be observed in Pascal's triangle that each row can be generated using the previous row. Pascal's triangle can be easily implemented using a 2d array.
Let us form an algorithm for this.
Algorithm:
- Start
- Define a function named printPascal that takes an integer n as input.
- Declare a 2d array of size nXn.
- Iterate over each line of the triangle from 0 to n-1 using for loop.
- Use another for loop to iterate for each element in each row from 0 to the line number.
- If the current element is the first or last, set it to 1.
- Otherwise, set it to the sum of the two elements above it.
- Print the current element.
- Print a line break.
- End the inner for loop.
- End the outer for loop.
- Define the main function.
- Call the printPascal function with an argument for the number of rows in Pascal's triangle.
- Exit.
Code:
#include<stdio.h> void printPascal(int n) { int arr[n][n]; for (int line = 0; line < n; line++) { for (int i = 0; i <= line; i++) { if (line == i || i == 0) arr[line][i] = 1; else arr[line][i] = (arr[line - 1][i - 1] + arr[line - 1][i]); printf("%d ", arr[line][i]); } printf("\n"); } } // Driver code int main() { int n = 5; printPascal(n); return 0; }
Output:
Using Combination
We can also calculate each newer value other than one by the nCr formula; n stands for the number of rows, and r for the number of columns.
Code:
#include <stdio.h> void printPascal(int n) { for (int line = 1; line <= n; line++) { int C = 1; for (int i = 1; i <= line; i++) { printf("%d ", C); C = C * (line - i) / i; } printf("\n"); } } // Driver code int main() { int n = 5; printPascal(n); return 0; }
Output:
We have seen all three ways to implement Pascal's triangle in the C programming language.