Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The formula for computing the number of ways of choosing r different things from

ID: 3812596 • Letter: T

Question

The formula for computing the number of ways of choosing r different things from a set of n things (the number
of r-combinations) is the following:
C(n, r) =
n!
r! (n r)!
In this formula, the factorial function is represented by an exclamation point (!) and defined as the product:
n! = n · (n 1) · (n 2) · ... · 1
Discover a recursive version of the C(n, r) formula and write a recursive method that computes the values of
the formula. The name of the method should be combinations .
For this method write a program that would test it. The program
should prompt the user to enter value(s) of parameter(s) and execute the method.

Explanation / Answer

Solution:

#include<bits/stdc++.h>
using namespace std;
long long int combinations(long long int n,long long int r)
{
if(r==0|r==n)
return 1;
else
return combinations(n-1,r-1)+combinations(n-1,r); //nCr = n-1Cr-1 + n-1Cr
}
int main()
{
   int t;
   cout<<"Enter the number of times you want to check nCr:"; //for checking t times
   cin>>t;
   while(t--)
   {
       long long int n,r,value;
       cout<<"Enter the values of n & r: ";
       cin>>n>>r;
       if(n<r)
       {
           cout<<"N shouldn't be less than r: "; //if n<r we should print error
       }
       else
       {
           value=combinations(n,r); //calling combinations function
           cout<<"Value of "<<n<<"C"<<r<<" is: "<<value<<endl;
       }
   }
}

Output:

Enter the number of times you want to check nCr:3
Enter the values of n & r: 5 9
N shouldn't be less than r:
Enter the values of n & r: 12 6
Value of 12C6 is: 924
Enter the values of n & r: 5 2
Value of 5C2 is: 10

Hope u understand.If not comment here.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote