Description
Permutation is the different arrangements of the set elements. The arrangements can be made by taking one element at a time, some element at a time and all elements at a time. Combination is the different selections of the set of elements taken one by one, or some, or all at a time.
C/C++
/* C Program to calculate Permutation And Combination */ //Save it as CalculatePermutationAndCombination.c #include<stdio.h> int main(){ int n,r; printf("Enter the value of n : "); scanf("%d",&n); printf("Enter the value of r : "); scanf("%d",&r); //Calculation of Permuatation int nPr = calculate_nPr(n, r); printf("nPr : %d\n", nPr); //Calculation of Combination int nCr = calculate_nCr(n, r); printf("nCr : %d", nCr); return 0; } //Function to calculate Permuatation int calculate_nPr(int n, int r) { int result; result = fact(n)/fact(n-r); return result; } //Function to calculate Combination int calculate_nCr(int n, int r) { int result; result = fact(n)/(fact(n-r)*fact(r)); return result; } //Function to calculate Factorial int fact(int n) { int i, factorial = 1; for(i=1;i<=n;i++) { factorial *= i; } return factorial; }
Input: Enter the value of n : 10 Enter the value of r : 5 Output: nPr : 30240 nCr : 252
Java
/* Java Program to calculate Permutation And Combination */ //Save it as CalculatePermutationAndCombination.java import java.io.*; import java.util.Scanner; public class CalculatePermutationAndCombination { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the value of n : "); int n = scanner.nextInt(); System.out.println("Enter the value of r : "); int r = scanner.nextInt(); //Calculation of Permuatation int nPr = calculate_nPr(n, r); System.out.println("nPr : "+nPr); //Calculation of Combination int nCr = calculate_nCr(n, r); System.out.println("nCr : "+nCr); } //Function to calculate Permuatation private static int calculate_nPr(int n, int r) { int result; result = fact(n)/fact(n-r); return result; } //Function to calculate Combination private static int calculate_nCr(int n, int r) { int result; result = fact(n)/(fact(n-r)*fact(r)); return result; } //Function to calculate Factorial private static int fact(int n) { int factorial = 1; for(int i=1;i<=n;i++) { factorial *= i; } return factorial; } }
Input: Enter the value of n : 10 Enter the value of r : 5 Output: nPr : 30240 nCr : 252
Related Programs
1) Add Two Numbers2) Program to subtract two numbers
3) Program to multiply two numbers
4) Program to divide two numbers
5) Program to find modulus of two numbers
6) Program to find Quotient and Remainder
7) Program to calculate power of a number
8) Program to find simple interest
9) Program to calculate Gross Salary
10) Program to calculate percentage mark of student