You're online now.

Hurray! you are online now.

# Write a C program to calculate the Binomial Coefficient (nCr)

nCr, is a mathematical notation representing the number of ways to choose r items from a set of n items, where order does not matter and repetitions are not allowed. It is also known as the binomial coeffiecient and is represented by the formula: nCr = n! / (r! * (n - r)!).

where n! represents the factorial of n, or the product of all positive integers up to n. The binomial coefficient nCr is often used in combinations, probability theory, and other areas of mathematics and statstics.

``````// write a c program to calculate nCr.
#include<stdio.h>

int factorial(int n){
int fact = 1;
for(int i = 1; i <= n; i++){
fact *= i;
}
return fact;
}

int nCr(int n, int r){
int nCr = factorial(n) / (factorial(r) * factorial(n - r));
return nCr;
}

int main(){
int n, r;
printf("Enter the value of n : ");
scanf("%d", &n);
printf("Enter the value of r : ");
scanf("%d", &r);
int result = nCr(n, r);
printf("nCr = %d", result);
return 0;
}``````
``#include<stdio.h>``

This line of code includes the standard input/output library, which provides the function for input and output operations.

``````// function to calculate factorial.
int factorial(int n){
int fact = 1;
for(int i = 1; i <= n; i++){
fact *= i;
}
return fact;
}``````

This function calculates the factorial of a given integer n, using a for loop that iterates from 1 o n, multiplying the variable "fact" by i at each iteration.

``````// function to calculate nCr.
int nCr(int n, int r){
int nCr = factorial(n) / (factorial(r) * factorial(n - r));
return nCr;
}``````

This function calculates the binomial coeffcient nCr, which is the number of ways to choose r items from a set of n items. It uses the factorial function to calculate the numerator and denominator of the binomial coefficient formula.

``````// main function (Entry point of program)
int main(){
int n, r;
printf("Enter the value of n : ");
scanf("%d", &n);
printf("Enter the value of r : ");
scanf("%d", &r);
int result = nCr(n, r);
printf("nCr = %d", result);
return 0;
}``````

This is the main function that calls the nCr function and reads input values for n and r using the scanf() function. It then computes the binomial coefficient using the nCr function and prints the result to the console using printf() function. The main function returns 0 to indicates successful execution of the program.

## Algorithm

1. Include the standard input/output library.
2. Define a function to calculate the factorial of a given integer n.
3. Define a function to calculate the binomial coeffiecient nCr using the factorial function.
4. In the main function, read input values for n and r using the scanf() function.
5. Compute the binomial coefficient using the nCr funciton.
6. Print the result to the console using printf.
7. End the program.

## Pseudocode

``````// Pseudocode to calculate nCr.
1. INCLUDE stdio.h
2. FUNCTION factorial(n)
1. SET fact = 1
2. FOR i = 1 to n
1. SET fact = fact * i
3. RETURN fact
3. FUNCTION nCr(n, r)
1. SET nCr = factorial(n) / (factorial(r) * factorial(n - r))
2. RETURN nCr
4. FUNCTION main()
1. DECLARE n, r, result AS INTEGER
2. PRINT "Enter the value of n : "