The nCr also termed as a combination. Combinations are a way to calculate the total outcomes of an event where the order of the outcomes does not matter. To calculate combinations, we will use the formula nCr = n! / r! * (n – r)! , where n represents the total number of items, and r represents the number of items being chosen at a time.
#include <stdio.h> int fact(int n) { int i; int res = 1; for (i = 2; i <= n; i++) { res = res * i; } return res; } int getnCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } int main() { int num, r; long ncrValue; printf("Enter the value of num = "); scanf("%d",&num); printf("Enter the value of r = "); scanf("%d",&r); ncrValue = getnCr(num, r); printf("%d C %d = %ld\n", num, r, ncrValue); return 0; }
Output:
Enter the value of num = 10
Enter the value of r = 4
10 C 4 = 210