Need the algorithm for Pascal's triangle in C
Answers
Answered by
2
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
kanakchaturvedi:
Is this the algorithm or the program itself for Pascal's triangle?
static long factorial (byte i)
{
if(i == 0 || i == 1)
return (long) 1;
else
return (long) i * factorial((byte) (i - 1));
}
}
class BinomialCoefficient {
static long binomialCoefficient(byte n, byte r){
long binCoeff = 0;
binCoeff = (long) (Factorial.factorial(n))/
(Factorial.factorial(r) * Factorial.factorial((byte) (n - r)));
return (long) binCoeff;
}
}
1 row = adding the two numbers above them to the left and the right
= (0+1) , (1+0)
= 1 , 1
2 row = (0+1) , (1+1) , (1+0)
= 1 , 2 , 1
3 row = (0+1), (1+2), (2+1), (1+0)
= 1 , 3 , 3 , 1
4 row = (0+1), (1+3) , (3+3), (3+1), (1+0)
= 1 , 4 , 6 , 4 , 1
Similar questions