Permutation and combination of 2 numbers

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.

Code Example

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();
    }
}

Code Explanation

Class Definition

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.

Method to Calculate Factorial of a Number

Method Signature
public static long factorial(int n) {

This method factorial takes an integer parameter n and returns a long value representing the factorial of n.

Base Case and Loop
if (n == 0) {
    return 1;
}
long fact = 1;
for (int i = 1; i <= n; i++) {
    fact *= i;
}
  • If n is 0, the method returns 1 because the factorial of 0 is 1.
  • fact is initialized to 1.
  • A for loop multiplies fact by each integer from 1 to n, calculating the factorial iteratively.

Method to Calculate Permutations (nPr)

Method Signature
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.

Calculating 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!​.

Method to Calculate Combinations (nCr)

Method Signature
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.

Calculating 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!​.

Main Method

Reading User Input
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.

Calculating and Displaying Permutations
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.

Calculating and Displaying Combinations
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.