I am trying Pascal Triangle solution in C, based on below formula:

I wrote the below code based on above formula:
#include <stdio.h>
#include <stdlib.h>
int pascalTriangle(int row, int col);
int main()
{
int row, col;
printf("Enter the row [0 to n]: ");
scanf("%i", &row);
printf("Enter the column [0 to m]: ");
scanf("%i", &col);
if(col > row) {
printf("Error: column can be less than or equal to row\n");
exit(1);
}
printf("Value = %i\n", pascalTriangle(row, col));
return 0;
}
int pascalTriangle(int row, int col)
{
int value[100];
value[0]=1;
int i=1;
if(row==0 || row==col || col==0) {
return value[0];
} else {
row=row+1;
while(i<=col) {
printf("i = %i\trow = %i\tcol = %i\n", i, row, col);
value[i]='\0';
value[i]=(value[i-1]) * ((row-i)/i);
printf("value[%i] = %i\tvalue[%i] = %i\n", i-1, value[i-1], i, value[i]);
++i;
}
return value[i-1];
}
}
Here, upto some extent it is giving proper O/P. But, for many I/Ps I found wrong answers. I am unable to find the logical error, as on paper the logic is giving expected O/P. Example:- I give row=4 & col=2, O/P should be 6 but getting 4 as O/P.
Please help!!
The line
is wrong.
row - ineed not be divisible byi(it generally isn’t). You need to first multiply and then divide,(parentheses not necessary since they are implicitly placed thus), but then you have earlier overflow, so for larger values of
row, compute the gcd,and divide
(row - i) / g,value[i-1]/(i/g)and multiply the results of these.