Permutations and combinations are two important concepts in combinatorial mathematics. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. A combination is a selection of all or part of a set of objects, without regard to the order of the selection.
This example demonstrates how to calculate permutations and combinations of two given numbers in Java. The program includes methods to calculate factorials, permutations, and combinations.
import java.util.Scanner;
public class PermutationCombination {
// Method to calculate factorial of a number
public static long factorial(int n) {
if (n == 0) {
return 1;
}
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
// Method to calculate permutations (nPr)
public static long permutation(int n, int r) {
return factorial(n) / factorial(n - r);
}
// Method to calculate combinations (nCr)
public static long combination(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter value for n: ");
int n = scanner.nextInt();
System.out.print("Enter value for r: ");
int r = scanner.nextInt();
// Calculate and display permutations
long perm = permutation(n, r);
System.out.println(n + "P" + r + " = " + perm);
// Calculate and display combinations
long comb = combination(n, r);
System.out.println(n + "C" + r + " = " + comb);
scanner.close();
}
}
public class PermutationCombination {
This line defines a new class named PermutationCombination
. This class contains methods to calculate factorials, permutations, and combinations, as well as the main method to execute the program.
public static long factorial(int n) {
This method factorial
takes an integer parameter n
and returns a long
value representing the factorial of n
.
if (n == 0) {
return 1;
}
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
n
is 0, the method returns 1 because the factorial of 0 is 1.fact
is initialized to 1.for
loop multiplies fact
by each integer from 1 to n
, calculating the factorial iteratively.public static long permutation(int n, int r) {
This method permutation
takes two integer parameters n
and r
, and returns a long
value representing the number of permutations.
return factorial(n) / factorial(n - r);
The method calculates permutations using the formula nPr=n!(n−r)!nPr = \frac{n!}{(n-r)!}nPr=(n−r)!n!.
public static long combination(int n, int r) {
This method combination
takes two integer parameters n
and r
, and returns a long
value representing the number of combinations.
return factorial(n) / (factorial(r) * factorial(n - r));
The method calculates combinations using the formula nCr=n!r!(n−r)!nCr = \frac{n!}{r!(n-r)!}nCr=r!(n−r)!n!.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter value for n: ");
int n = scanner.nextInt();
System.out.print("Enter value for r: ");
int r = scanner.nextInt();
These lines create a Scanner
object to read input from the user and prompt the user to enter values for n
and r
.
long perm = permutation(n, r);
System.out.println(n + "P" + r + " = " + perm);
These lines calculate the permutations using the permutation
method and display the result.
long comb = combination(n, r);
System.out.println(n + "C" + r + " = " + comb);
These lines calculate the combinations using the combination
method and display the result.