Wednesday 22 November 2017

C++ Program to Compute Combinations using Factorials


Code:

#include    iostream

using namespace std;

// A function to find the factorial.
int factorial(int n)
{
int i;
for(i = n-1; i > 1; i--)
n *= i;

return n;
}

int main()
{
int n, r,  result;
cout<<"A program to find combination from nCr format using ( n! / (r! * (n-r)!))";
cout<<"\n\n\tEnter the value of n: ";
cin>>n;
cout<<"\tEnter the value of r: ";
cin>>r;

// Get result using formula to calculate combinations.
result = factorial(n)/(factorial(r)*factorial(n-r));

cout<<"\nThe number of possible combinations is: "<
}


Output:

Case 1:
A program to find combination from nCr format using ( n! / (r! * (n-r)!))

        Enter the value of n: 5
        Enter the value of r: 3

The number of possible combinations is: 10

Case 2:
A program to find combination from nCr format using ( n! / (r! * (n-r)!))

        Enter the value of n: 13
        Enter the value of r: 4

The number of possible combinations is: 221



More C++ Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...