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