In this tutorial you will learn about the C Program to find ncr and npr and its application with practical example.
In this tutorial, we will learn to create a C program that will Calculate Permutation(nPr) and Combination(nCr) using C programming.
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Operators.
- Functions in C.
- C Recursion.
What Is Permutation (nPr) and Combination (nCr)?
A permutation is an ordered arrangement . The term arrangement is referred if the order of things is considered.
Formula. nPr = n!/(n-r)!
Note->Permutation shows arrange r things out of n.
What is Combination?
“Combination” is any selection or pairing of values within a single category
i.e combination means selection of things, Order of objects does not matter.
Note->Combination shows to select r things out of n.
C Program to find ncr and npr.
long factorial(int n) // function for factorial
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
long find_combination(int n, int r) // function for combination
com = factorial(n)/(factorial(r)*factorial(n-r));
long find_permutation(int n, int r) // function for permutation
p = factorial(n)/factorial(n-r);
int n, r;
long ncr, npr; // declaring variables
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r); // taking values form user
ncr = find_combination(n, r); // passing arguments to functions
npr = find_permutation(n, r);
printf("%dC%d = %ld\n", n, r, ncr); // printing combination
printf("%dP%d = %ld\n", n, r, npr); // printing permutation
In the above program, we have first declared and initialized a set variables required in the program.Firstly we ask user to give the value of n,r by prompting user and by getting the value we pass the value or n and r to function find_combination(n,r)
This function take two parameter while calling and within function we call another function named factorial that will find the possible combination of n and r and at the end this function returns total combination back to caller object and same process we will use for permutation.
This function take two parameter while calling and within function we call another function named factorial that will find the possible permutation of n and r and at the end this function returns total permutation back to caller object and at end we will print total number of combination and permutation.