Q:

Write a C program to calculate the value of nCr

belongs to collection: C Programming Examples

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C program to calculate the value of rPr... >>
<< C program to check whether the triangle is valid o...