C Program: Print/Generate Pascal's Triangle

Pascal's Triangle is a triangular array of numbers which are the coefficients in the expansion of $(x + y)^{n}$.

The first row corresponds to $n = 0$, the second row to $n = 1$, and so on.

c program pascal triangle

You will notice that each row has 1 as its first and last number. The rest of the numbers are the sum of the immediate left and right numbers in the row above it.

The triangle is named after Blaise Pascal, the famous French mathematician, physicist, and Jansenist apologist, who wrote an important treatise in projective geometry at the tender age of sixteen. The SI unit of pressure is named after him.

blaise pascal Blaise Pascal. Public Domain

Now, the Binomial Theorem states that, in the expansion of $(x + y)^{n}$, the coefficient of the $k$-th term can be expressed as $\frac{n!}{k!(n-k)!}$, which is denoted by $_{n} \text{C}^{k}$. We usually say it as "$n$ choose $k$". These coefficients are known as binomial coefficients.

In our below C program, a separate function nCr() is created just for computing the binomial coefficient. We will try to get the coefficient values correctly first for each row, without concerning the presentation.

				
				#include <stdio.h>

				unsigned long nCr(unsigned short n, unsigned short r);
				unsigned long factorial(unsigned short int);

				int main() {
					unsigned short int n, r, i;

					printf("rows: ");
					scanf("%hu", &n);

					r = 0;

					while(r < n) {
						for(i = 0; i <= r; i++) {
							printf("%lu	", nCr(r,i));
						}

						printf("\n");
						r++;
					}


					printf("\n");

					return 0;
				}

				unsigned long nCr(unsigned short n, unsigned short r) {
					return factorial(n)/(factorial(r)*factorial(n-r)); 
				}

				unsigned long factorial(unsigned short n) {
					unsigned long f;
					
					if(n == 0 || n == 1)
						return 1;
					else 
						f = n * factorial(n-1);

					return f;
				}
				
			

Below is the output of the program upto 6 rows:

c program pascal triangle 6 rows

Now that we have the correct coefficients for each row, what matters next is the spacing between the coefficients to display it in the shape of an isosceles triangle; a kind of indentation to be applied per row. We can achieve it with some clever use of format specifiers.

To display the triangle upto 20 rows, where the values of the coefficients can reach upto 5-digits, the format specifier has to be set to "%6lu", that one additional space being for spacing between the coefficients. And the printf(" ") statement above it should be set to three spacebars. The 21st row, however, will give no space between the coefficients, as it contains 6-digit binomial coefficients. $_{20} \text{C}^{10} = 184756$, which is a six-digit value.

				
				#include <stdio.h>

				unsigned long nCr(unsigned short n, unsigned short r);
				unsigned long factorial(unsigned short int);

				int main() {
					unsigned short int n, r, i, j;

					printf("rows: ");
					scanf("%hu", &n);

					r = 0;

					while(r < n) {
						for(i = (n-r+1); i > 0; i--) 
							printf("   ");

						for(j = 0; j <= r; j++) {
							printf("%6lu", nCr(r,j));
						}

						printf("\n");
						r++;
					}


					printf("\n");

					return 0;
				}

				unsigned long nCr(unsigned short n, unsigned short r) {
					return factorial(n)/(factorial(r)*factorial(n-r)); 
				}

				unsigned long factorial(unsigned short n) {
					unsigned long f;
					
					if(n == 0 || n == 1)
						return 1;
					else 
						f = n * factorial(n-1);

					return f;
				}
				
			

Below is the result of the above program, executed upto 20 rows.

c program pascal triangle 20 rows