Calculating nCr using recursion

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; }     
 

6 comments:

  1. Appreciation for nice Updates, I found something new and folks can get useful info about BEST ONLINE TRAINING

    ReplyDelete
  2. Will it work if we write
    res=(float)(n/r)*ncr(n-1,r-1);
    ?

    ReplyDelete
    Replies
    1. Yes, it does. I just tried that out.
      But, 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);

      Delete
  3. what will be the time complexity of this code??

    ReplyDelete