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))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
Really thanks so much! SO MUCH!