Write a C Program for Pascal’s triangle

Admin
Write a C Program for Pascal’s triangle
#include<stdio.h>
long factorial(int);
long comb(int,int);
int main(void)
{
	int i,j,k;
	printf("Enter number of rows  for Pascal's triangle : ");
	scanf("%d",&k);
	for(i=0; i<k; i++)
	{
		for(j=0; j<=i; j++)
			printf("%5ld",comb(i,j));
		printf("\n");
	}
	return 0;
}
long comb(int n,int r)
{
	long c;
	c=factorial(n)/(factorial(r)*factorial(n-r));
	return c;
}
long int factorial(int n)
{
	int i;
	long int fact=1;
	if(n==0)
		return 1;
	for(i=n; i>1; i--)
		fact*=i;
	return fact;
}
Output
Enter number of rows  for Pascal's triangle : 6
    1
    1    1
    1    2    1
    1    3    3    1
    1    4    6    4    1
    1    5   10   10    5    1

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.