Why is my solution to binomial coefficient crashing? I've really tried to learn recursion, but I still think I am not clear about about it. I wonder if anyone can help me learn about recursion, and how to think recursively?
Even when I write a good base case, my program crashes. Any link to learn recursion with clear demonstration would be very helpful for me.
Here is my code for binomial coefficient, and I'm unable to find the bug/error, looking for your help.
code:
#include<stdio.h>
long long m,n,o,p,result;
long long binomial(long long n,long long m)
{
if(n==m)
return 1;
else {
if(m==0)
return 1;
else {
o=binomial(n-1,m);
p=binomial(n-1,m-1);
return o+p;
}
}
}
int main()
{
printf("Please Enter The Value Of n:\n");
scanf("%lld",&n);
printf("Now Enter The value of m:\n");
scanf("%lld",&m);
result = binomial(n,m);
printf("Resultant Binomial coefficient: %lld\n",result);
return 0;
}