/*Write a C program to generate Pasal's Triangle*/
#include<stdio.h>
#include<conio.h>
long factorial(int);
main()
{
int i,j,n;
/*clear screen logic*/
for(i=0;i<50;i++)
printf("\n");
gotoxy(1,1);
/*clear screen login end*/
printf(" Pascal Triangle\n");
printf("Enter Range:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<n-i;j++)
printf(" ");
for(j=0;j<=i;j++)
{
printf("%ld ", factorial(i)/(factorial(i-j)*factorial(j)));
}
printf("\n");
}
getch();
}
long factorial(int n)
{
int i;
long int result=1;
if(n==0)
return 1;
else
{
for(i=1;i<=n;i++)
result=result*i;
return (result);
}
}
#include<conio.h>
long factorial(int);
main()
{
int i,j,n;
/*clear screen logic*/
for(i=0;i<50;i++)
printf("\n");
gotoxy(1,1);
/*clear screen login end*/
printf(" Pascal Triangle\n");
printf("Enter Range:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<n-i;j++)
printf(" ");
for(j=0;j<=i;j++)
{
printf("%ld ", factorial(i)/(factorial(i-j)*factorial(j)));
}
printf("\n");
}
getch();
}
long factorial(int n)
{
int i;
long int result=1;
if(n==0)
return 1;
else
{
for(i=1;i<=n;i++)
result=result*i;
return (result);
}
}
output:
Pascal Triangle
Enter Range:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Enter Range:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
0 comments:
Post a Comment