Write a C program to generate Pascal’s triangle.

Algorithm:

Step 1: Start
Step 2: Read p value as integer
Step 3: while(q<p)
         begin
            for  r:40 - 3 * q to 0  decrement r by 1
               for x: 0 to q increment x by 1
               begin
                  if((x == 0) || (q == 0))
                      binom = 1;
                  else
                      binom = (binom * (q - x + 1)) / x;
                      print binom
                end
                ++q;
          end
Step 4: Stop

Program:

#include <stdio.h>
#include <conio.h>
long factorial(int);

void main()
{
   int i, n, c;
   clrscr();
   printf("Enter the number of rows\n");
   scanf("%d",&n);

   for (i = 0; i < n; i++)
   {
      for (c = 0; c <= (n - i - 2); c++)
      {
     printf(" ");
      }
      for (c = 0 ; c <= i; c++)
      {
     printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
      }
      printf("\n");
   }
   getch();
}

long factorial(int n)
{
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
   {
      result = result*c;
   }
   return result;
}

Input & Output:

Enter the number of rows
5
             1
         1      1
      1     2     1
   1     3     3     1
1     4     6     4     1