In this code example the permutation and combination of 2 numbers are calculated.
import java.util.Scanner;
public class PermutationCombination {
private static int fact(int val) {
if (val == 0 || val == 1)
return 1;
else
return val * fact(val - 1);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n, r, combination, permutation;
System.out.print("Enter value of n : ");
n = scan.nextInt();
System.out.print("Enter value of r : ");
r = scan.nextInt();
combination = fact(n) / (fact(r) * fact(n - r));
permutation = fact(n) / fact(n - r);
System.out.println("\nCombination : " + combination);
System.out.print("Permutation : " + permutation);
}
}
Enter value of n : 8
Enter value of r : 6
Combination : 28
Permutation : 20160