Java programs to find the value of $nPr$ and $nCr$ have been shown here. $nPr$, $nCr$ are referred as the permutation and combination respectively. Here $n$ is the total no of objects, $r$ is the no of objects taken at a time for arrangement and $r \leq n$. The formula to find $nPr$ and $nCr$ are given below.
$nPr = \frac{n!}{(n-r)!}$
$nCr = \frac{n!}{(n-r)! \cdot r!}$
Example:
Suppose $n = 7$ and $r = 5$
$7P5 = \frac{7!}{(7-5)!} = \frac{7!}{2!} = \frac{5040}{2} = 2520$
$7C5 = \frac{7!}{(7-5)!\cdot5!} = \frac{7!}{2!\cdot5!} = \frac{5040}{240} = 21$
1. Program & output to find the value of permutation (nPr) and combination (nCr)
1.1. Java Program & output to find the value of permutation (nPr) and combination (nCr) using iteration
Code has been copied
/******************************** alphabetacoder.com Java Program to find permutation (nPr) and combination (nCr) *********************************/ import java.util.Scanner; import java.lang.*; class Main { // function to calculate factorial long factorial(int num) { // declare variables int i; long fact = 1; // calculate factorial for (i = 2; i <= num; i++) fact *= i; // return result return fact; } // function to calculate nCr long calculate_ncr(int n, int r) { // return result return factorial(n) / (factorial(n - r) * factorial(r)); } // function to calculate nPr long calculate_npr(int n, int r) { // return result return factorial(n) / factorial(n - r); } public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare object of class Main obj = new Main(); // delclare variables int n, r; // take input of the number System.out.print("Enter the value of n: "); n = sc.nextInt(); System.out.print("Enter the value of r: "); r = sc.nextInt(); // display results by calling function System.out.println(n + "P" + r + " = " + obj.calculate_npr(n, r)); System.out.println(n + "C" + r + " = " + obj.calculate_ncr(n, r)); } }
Output
Enter the value of n: 9
Enter the value of r: 2
9P2 = 72
9C2 = 36
1.2. Java Program & output to find the value of permutation (nPr) and combination (nCr) using recursion
Code has been copied
/************************************ alphabetacoder.com Java Program to find permutation(nPr) and combination (nCr) using recursion *************************************/ import java.util.Scanner; import java.lang.*; class Main { // recursive function to calculate factorial long factorial(int num) { if (num <= 1) return 1; return num * factorial(num - 1); } // function to calculate nCr long calculate_ncr(int n, int r) { // return result return factorial(n) / (factorial(n - r) * factorial(r)); } // function to calculate nPr long calculate_npr(int n, int r) { // return result return factorial(n) / factorial(n - r); } public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare object of class Main obj = new Main(); // delclare variables int n, r; // take input of the number System.out.print("Enter the value of n: "); n = sc.nextInt(); System.out.print("Enter the value of r: "); r = sc.nextInt(); // display results by calling function System.out.println(n + "P" + r + " = " + obj.calculate_npr(n, r)); System.out.println(n + "C" + r + " = " + obj.calculate_ncr(n, r)); } }
Output
Enter the value of n: 7
Enter the value of r: 0
7P0 = 1
7C0 = 1