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.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Operators.
- Functions in C.
- Factorial.
- C Recursion.
What Is Permutation (nPr) and Combination (nCr)?
Permutation:=>
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.
Formula. n!/(r!*(n-r)!).
Note->Combination shows to select r things out of n.
C Program to find ncr and npr.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> long factorial(int n) // function for factorial { int c; long result = 1; for (c = 1; c <= n; c++) result = result*c; return result; } long find_combination(int n, int r) // function for combination { long com; com = factorial(n)/(factorial(r)*factorial(n-r)); return com; } long find_permutation(int n, int r) // function for permutation { long p; p = factorial(n)/factorial(n-r); return p; } int main(){ 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 return 0; } |
Output
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.