You are here

Pascal triangle C program

Pascal triangle C program: C program to print Pascal triangle which you might have studied while studying Binomial Theorem in Mathematics. A user will enter how many numbers of rows to print. It's four rows are:

   1
  1 1
 1 2 1
1 3 3 1

Pascal triangle in C

  1. #include <stdio.h>
  2.  
  3. long factorial(int);
  4.  
  5. int main()
  6. {
  7.    int i, n, c;
  8.  
  9.    printf("Enter the number of rows you wish to see in pascal triangle\n");
  10.    scanf("%d",&n);
  11.  
  12.    for (i = 0; i < n; i++)
  13.    {
  14.       for (c = 0; c <= (n - i - 2); c++)
  15.          printf(" ");
  16.  
  17.       for (c = 0 ; c <= i; c++)
  18.          printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
  19.  
  20.       printf("\n");
  21.    }
  22.  
  23.    return 0;
  24. }
  25.  
  26. long factorial(int n)
  27. {
  28.    int c;
  29.    long result = 1;
  30.  
  31.    for (c = 1; c <= n; c++)
  32.          result = result*c;
  33.  
  34.    return result;
  35. }

Pascal triangle C program output:
Pascal triangle C program output

Download Pascal triangle program.

For more patterns or shapes on numbers and characters see codes on following pages:
Patterns programs
Floyd triangle