Print Pascal triangle in C

In this article we will print Pascal triangle in C programming. You may have heard or studied about pascal triangle when studying Binomial theorem in Mathematics. Here we will print pascal triangle after taking the number of row from the user. So, let’s see the program for printing pascal triangle in C.

Before getting started to code we should know how a Pascal triangle looks like. See the pascal triangle bellow of first 6 rows.

      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
1 5 10 10 5 1

C program to print Pascal triangle

// C program to print Pascal triangle
#include <stdio.h>

long fact(int a){
  int k;
  long result = 1;

  for (k = 1; k <= a; k++){
    result = result * k;
  }
return result;
}

int main(){
  int num, i, j;

  printf("Enter the number of rows to print pascal triangle : ");
  scanf("%d", &num);

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

    for (j = 0; j <= (num - i - 2); j++){
      printf(" ");
    }

    for (j = 0 ; j <= i; j++){
      printf("%ld ", fact(i) / (fact(j) * fact(i - j)));
    }

    printf("\n");
  }

  return 0;
}

Output of Pascal triangle program:

pascal triangle, c program to print pascal triangle

Previous pageFloyd’s triangle in C

Next pageCount digit program by C