C Program to Print Pascal Triangle

In this tutorial, i am going to show you how to print pascal triangle with the help of for loop and recursion in c programs.

All C Programs to Print Pascal Triangle in C

  • C Program to Print Pascal Triangle using For Loop
  • C Program to Print Pascal Triangle using Recursion

C Program to Print Pascal Triangle using For Loop

#include <stdio.h>
 
long Factorial(int);
 
int main()
{
   int i, Number, j;
 
   printf("Enter number of rows : ");
   scanf("%d", &Number);
 
   for (i = 0; i < Number; i++)
   {
      for (j = 0; j <= (Number - i - 2); j++)
      {
      	printf(" ");
	  }
 
      for (j = 0; j <= i; j++)
      {
      	printf("%ld ", Factorial(i) / (Factorial(j) * Factorial(i-j)));
	  } 
 
      printf("\n");
   }
 
   return 0;
}
 
long Factorial(int Number)
{
   int i;
   long Fact = 1;
 
   for (i = 1; i <= Number; i++)
         Fact = Fact * i;
 
   return Fact;
}

The result of the above c program; is as follows:

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

C Program to Print Pascal Triangle using Recursion

#include <stdio.h>
long Factorial(int Number)
{
  if (Number == 0 || Number == 1)  
    return 1;
  else
    return Number * Factorial (Number -1);
}
int main()
{
   int i, Number, j, Fact;
   printf("Enter number of rows : ");
   scanf("%d", &Number);
   for (i = 0; i < Number; i++)
   {
      for (j = 0; j <= (Number - i - 2); j++)
      {
      	printf(" ");
      }
      for (j = 0; j <= i; j++)
      {
      	Fact = Factorial(i) / (Factorial(j) * Factorial(i-j));
	printf("%ld ", Fact);
      } 
      printf("\n");
   }
   return 0;
}

The result of the above c program; is as follows:

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

More C Programming Tutorials

Leave a Comment