Problem Statement
Write a c program to calculate nCr using recursion
#include<stdio.h>
int ncr(int n,int r);
int main()
{
int n,r,y;
printf("enter the value of n and r resp.\n");
scanf("%d %d",&n,&r);
y=ncr(n,r);
printf("the value of ncr is %d\n",y);
return 0;
}
int ncr(int n,int r)
{
int y,res;
if(r==0)
{
y=1;
return y;
}
else if(n==r)
{
y=1;
return y;
}
else
{
res=ncr(n-1,r-1)+ncr(n-1,r);
}
return res;
}
Appreciation for nice Updates, I found something new and folks can get useful info about BEST ONLINE TRAINING
ReplyDeleteExplain
ReplyDeleteExplain it how it's work
ReplyDeleteWill it work if we write
ReplyDeleteres=(float)(n/r)*ncr(n-1,r-1);
?
Yes, it does. I just tried that out.
DeleteBut, you gotta enter the (/r) part of the expression later and then you won't need type casting.
res=(n * ncr(n-1,r-1) / r);
what will be the time complexity of this code??
ReplyDelete