C Program to Print Pascal Triangle

C Program to Print Pascal Triangle

Program to print pascal triangle in c; Through this tutorial, we will learn how to print pascal triangle using for loop and recursion in c programs.

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 output 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 output 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 

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *