Combination
Combination is a way of selecting several things out of a larger group, where (unlike Permutation) order does not matter. In smaller cases it is possible to count the number of combinations. For example given three fruit, an apple, orange and pear say, there are three combinations of two that can be drawn from this set: an apple and a pear; an apple and an orange; or a pear and an orange.
Below is the code for calculating total number of combinations.
Code
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
long GetFact(long number)
{
long factorial=1;
for(int i=number;i>0;i--)
factorial=factorial*i;
return factorial;
}
long nCr(long n,long r)
{
long t=GetFact(n);
long u=GetFact(n-r);
long v=GetFact(r);
return (t/(u*v));
}
int main()
{
cout<<nCr(5,0);cout<<endl;
cout<<nCr(5,1);cout<<endl;
cout<<nCr(5,2);cout<<endl;
cout<<nCr(5,3);cout<<endl;
cout<<nCr(5,4);cout<<endl;
cout<<nCr(5,5);cout<<endl;
_getche();
return 0;
}
Output
1
5
10
10
5
1
Related article
0 comments:
Post a Comment