# Program to calculate value of nCr in C++

C++Server Side ProgrammingProgramming

Given with n C r, where C represents combination, n represents total numbers and r represents selection from the set, the task is to calculate the value of nCr.

Combination is the selection of data from the given in a without the concern of arrangement. Permutation and combination differs in the sense that permutation is the process of arranging whereas combination is the process of selection of elements from the given set.

Formula for permutation is -:

nPr = (n!)/(r!*(n-r)!)

## Example

Input-: n=12 r=4
Output-: value of 12c4 is :495

## Algorithm

Start
Step 1 -> Declare function for calculating factorial
int cal_n(int n)
int temp = 1
Loop for int i = 2 and i <= n and i++
Set temp = temp * i
End
return temp
step 2 -> declare function to calculate ncr
int nCr(int n, int r)
return cal_n(n) / (cal_n(r) * cal_n(n - r))
step 3 -> In main()
declare variable as int n = 12, r = 4
print nCr(n, r)
Stop

## Example

#include <bits/stdc++.h>
using namespace std;
//it will calculate factorial for n
int cal_n(int n){
int temp = 1;
for (int i = 2; i <= n; i++)
temp = temp * i;
return temp;
}
//function to calculate ncr
int nCr(int n, int r){
return cal_n(n) / (cal_n(r) * cal_n(n - r));
}
int main(){
int n = 12, r = 4;
cout <<"value of "<<n<<"c"<<r<<" is :"<<nCr(n, r);
return 0;
}

## Output

value of 12c4 is :495
Updated on 23-Sep-2019 06:37:22