Java program to find nCr and nPr. Here, we will discuss the various methods to permutations and combinations using Java. The compiler has been added so that you can execute the programs yourself, alongside suitable examples and sample outputs. The methods discussed are:
Permutation: is the process of arrangement of all the members in any set into a particular sequence or order.
For example, set A with members 1 & 2 can be arranged into 2 ways such as {1,2,} and {2,1}.
Combination: is the process of selection of members from any particular set.
For example, if we had to select 2 members at random from set A with members 1, 2 & 3, we’d have 3 possibilities namely {1,2}, {2,3} and {1,3}.
nCr & nPr – Using Function
1) In this program we have fact, permutation, combination functions to calculate ncr and npr.
2) Read n,r values using scanner object sc.nextInt(), and store it int the variables n,r.
p.permutation(n,r) then it calls the permutation method and permutation method calls the fact method and prints the permutation.
p.combination(n,r) then calls the combination method and combination method calls the fact method and prints the combination.
3) Repeats until the condition if(n>=r) is false.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.Scanner; class NcrAndNpr { double fact(double n) { int i=1; double fact=1; while(i<=n) { fact=fact*i; i++; } return fact; } double permutation(int n,int r ) { double per=fact(n)/fact(n-r); return per; } double combination(int n,int r) { double com=fact(n)/(fact(n-r)*fact(r)); return com; } public static void main(String arg[]) { NcrAndNpr p=new NcrAndNpr( ); Scanner sc=new Scanner(System.in); System.out.println("enter value of n"); int n=sc.nextInt(); System.out.println("enter value of r"); int r=sc.nextInt(); if(n>=r) { System.out.println("The value of "+n+"p"+r+" is : "+p.permutation(n,r)); System.out.println("The value of "+n+"c"+r+" is : "+p.combination(n,r)); } else System.out.println("n value should be greater than or equals to r value"); } } |
Output:
1 2 3 4 5 6 |
enter value of n 12 enter value of r 6 The value of 12p6 is : 665280.0 The value of 12c6 is : 924.0 |
Using Static Method
1) JVM runs the static block then static method then it creates an object for the class.
2) In this program, static double ncr(int n,int r), static double npr(int n,int r), public static void main(String arg[]). These 3 static methods are available.
3) Calls the two static methods ncr(),npr() in main method. The methods ncr(),npr() calls the fact() method using NcrAndNpr class object.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.Scanner; class NcrAndNpr { double fact(double n) { int i=1; double fact=1; while(i<=n) { fact=fact*i++; } return fact; } public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter value of n"); int n=sc.nextInt(); System.out.println("Enter value of r"); int r=sc.nextInt(); if(n>=r) { System.out.println("The value of "+n+"p"+r+" is : "+npr(n,r)); System.out.println("The value of "+n+"c"+r+" is : "+ncr(n,r)); } else System.out.println("Please enter n>=r"); } static double ncr(int n,int r) { NcrAndNpr p=new NcrAndNpr( ); double nc=p.fact(n)/(p.fact(n-r)*p.fact(r)); return nc; } static double npr(int n,int r) { NcrAndNpr p=new NcrAndNpr( ); double np=p.fact(n)/p.fact(n-r); return np; } } |
Output:
1 2 3 4 5 6 |
Enter value of n 5 Enter value of r 2 The value of 5p2 is : 20.0 The value of 5c2 is : 10.0 |
Using For Loop
1) Read the n,r values using scanner object sc.nextInt() and store it in the variables n,r.
2) We will calculate combination using this formula com=a.fact(n)/(a.fact(n-r)*a.fact(r));
3) Permutation using this formula per=a.fact(n)/a.fact(n-r); until n>=r is false.
4) The fact method will calculate the factorial of a number using for loop. For loop iterates from i=0 to i=n, for each iteration f=f*i will be calculated and the result will be returned.
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 27 28 29 30 31 |
import java.util.Scanner; class NcrAndNpr { double fact(double n) { double f=1; for(int i=1;i<=n;i++) { f=f*i; } return f; } public static void main(String arg[]) { NcrAndNpr a=new NcrAndNpr(); Scanner sc=new Scanner(System.in); System.out.println("Enter value of n"); int n=sc.nextInt(); System.out.println("Enter value of r"); int r=sc.nextInt(); if(n>=r) { double com=a.fact(n)/(a.fact(n-r)*a.fact(r)); double per=a.fact(n)/a.fact(n-r); System.out.println("The value of "+n+"p"+r+" is : "+per); System.out.println("The value of "+n+"c"+r+" is : "+com); } else System.out.println("Please enter n>=r"); } } |
Output:
1 2 3 4 5 6 |
Enter value of n 10 Enter value of r 5 The value of 10p5 is : 30240.0 The value of 10c5 is : 252.0 |
Using Recursion
In case if you have no idea what is recursion, then do check out the complete Guide here.
1) In this program factorial(double n) call itself as factorial(n-1), it returns 1 if n=0 otherwise it returns (n *factorial(n-1))
2) We will calculate combination using this formula com=a.factorial(n)/(a.factorial(n-r)*a.factorial(r)); and permutation using this formula per=a.factorial(n)/a.factorial(n-r); until n>=r is false.
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 27 28 29 30 |
import java.util.Scanner; class NcrAndNpr { double factorial(double n) { if (n == 0) return 1; else return(n * factorial(n-1)); } public static void main(String arg[]) { NcrAndNpr a=new NcrAndNpr(); Scanner sc=new Scanner(System.in); System.out.println("Enter value of n"); int n=sc.nextInt(); System.out.println("Enter value of r"); int r=sc.nextInt(); if(n>=r) { double com=a.factorial(n)/(a.factorial(n-r)*a.factorial(r)); double per=a.factorial(n)/a.factorial(n-r); System.out.println("The value of "+n+"p"+r+" is : "+per); System.out.println("The value of "+n+"c"+r+" is : "+com); } else System.out.println("Please enter n>=r"); } } |
Output:
1 2 3 4 5 6 |
Enter value of n 10 Enter value of r 2 The value of 10p2 is : 90.0 The value of 10c2 is : 45.0 |