fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <math.h>
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. int x,y,i,j,k,l,dp[21][21];
  8.  
  9. long C(long n,long r)
  10. {
  11. if(n==r || r==0)
  12. return 1;
  13.  
  14. else if(dp[n][r]!=-1)
  15. return dp[n][r];
  16. else
  17. {
  18. dp[n][r]=C(n-1,r)+C(n-1,r-1);
  19. return dp[n][r];
  20. }
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26. long n,r,s;
  27.  
  28. printf("Enter n & r:\n");
  29. while(scanf("%ld%ld",&n,&r)==2)
  30. {
  31.  
  32.  
  33. memset(dp,-1,sizeof(dp));
  34.  
  35. s=C(n,r);
  36. cout<<endl<<"nCr"<<" = "<<s<<endl;
  37.  
  38.  
  39. printf("\nEnter n & r:\n");
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45.  
  46.  
Success #stdin #stdout 0s 3100KB
stdin
4 3
stdout
Enter n & r:

nCr = 4

Enter n & r: