Write a Java program to find the permutations and combinations of a given value with an example. This program accepts the n and r values and calculates the permutations and combinations using the math formula.
Perm = factorial(n) / factorial(n – r)
Comb = factorial(n) / (factorial(n – r) * factorial(r))
For more Java Programs >> Click Here.
package SimpleNumberPrograms;
import java.util.Scanner;
public class Permutations {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int n, r, NCR, NPR;
System.out.print("Please Enter n Value = ");
n = sc.nextInt();
System.out.print("Please Enter r Value = ");
r = sc.nextInt();
NCR = fact(n)/(fact(n - r) * fact(r));
NPR = fact(n)/fact(n - r);
System.out.println("NCR Value = " + NCR);
System.out.println("NPR Value = " + NPR);
}
public static int fact(int number) {
if(number == 0 || number == 1) {
return 1;
}
else {
return number * fact(number - 1);
}
}
}
