In this tutorial, we will see java program to find permutation and combination.
Permutation is represented as nPr and Combination is represented as nCr.It is simple program which involves factorial of the numbers.
Here is formula to find permutation and combination (nCr nPr).
nPr = factorial(n) / factorial(n-r)
nCr = factorial(n)/(factorial(n-r) * factorial(r))
nCr = factorial(n)/(factorial(n-r) * factorial(r))
import java.util.Scanner;
public class PermutationCombinationMain
{
public static int fact(int num){
int fact=1, i;
for(i=1; i<=num; i++){
fact = fact*i;
}
return fact;
}
public static void main(String args[]){
int n, r;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Value of n : ");
n = scanner.nextInt();
System.out.print("Enter Value of r : ");
r = scanner.nextInt();
System.out.print("NCR is " +(fact(n)/(fact(n-r)*fact(r))));
System.out.print("\nNPR is " +(fact(n)/(fact(n-r))));
}
}
Output:
Enter Value of n : 8
Enter Value of r : 4
NCR is 70
NPR is 1680
Enter Value of r : 4
NCR is 70
NPR is 1680
Really thanks so much! SO MUCH!