Advertisement
binibiningtinamoran

Simple Permutation

Jun 6th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Permutations {
  4.  
  5.     public static void main(String []args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.         System.out.print("Enter first number: ");
  9.         int inputA = scan.nextInt();
  10.         System.out.print("Enter second number: ");
  11.         int inputB = scan.nextInt();
  12.  
  13.         System.out.println(permute(inputA, inputB));
  14.  
  15.     }
  16.  
  17.     public static int permute(int a, int b) {
  18.         if (a < b) throw new IllegalArgumentException("First number must be >= second number.");
  19.         return getFactorial(a) / getFactorial(a-b);
  20.     }
  21.  
  22.     public static int getFactorial(int a) {
  23.         if (a < 0) throw new IllegalArgumentException("Number must be >= 0");
  24.         return a == 0 || a == 1 ? 1 : a * getFactorial(a-1);
  25.     }
  26.  
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement