Q:

C program to calculate the value of nCr

0

C program to calculate the value of nCr

All Answers

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

Read the value of n and r and calculate the nCr.

nCr:

nCr known as combination, is the method of selection of 'r' objects from a set of 'n' objects where the order of selection does not matter.

To find the value of nCr, we use the formula: nCr = n!/[r!( n-r)!]

Program:

The source code to calculate the value of nCr is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to calculate the value of nCr

#include <stdio.h>

int getFactorial(int num)
{
    int f = 1;
    int i = 0;

    if (num == 0)
        return 1;

    for (i = 1; i <= num; i++)
        f = f * i;

    return f;
}

int main()
{
    int n = 0;
    int r = 0;

    int nCr = 0;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    printf("Enter the value of R: ");
    scanf("%d", &r);

    nCr = getFactorial(n) / (getFactorial(r) * getFactorial(n - r));

    printf("The nCr is: %d\n", nCr);

    return 0;
}

Output:

RUN 1:
Enter the value of N: 7
Enter the value of R: 3
The nCr is: 35

RUN 2:
Enter the value of N: 5
Enter the value of R: 0
The nCr is: 1

RUN 3:
Enter the value of N: 0
Enter the value of R: 5
The nCr is: 0

RUN 4:
Enter the value of N: 11
Enter the value of R: 5
The nCr is: 462

Explanation:

In the above program, we created two functions getFactorial()main(). The getFactorial() function is used to find the factorial of the given number and return the result to the calling function.

In the main() function, we read the values of n and r. Then we calculate the nCr with the help of the getFactorial() function and print the result on the console screen.

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

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C program to calculate the value of nPr... >>
<< C program to read coordinate points and determine ...