Advertisement
philcrafts

BigInteger Adam/Phil

Oct 27th, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. /*
  2.  * Name:Philip Crafts/Adam Tiago
  3.  * Date:10/27/2017
  4.  * Course Number:CSC-220
  5.  * Course Name:Data Structure
  6.  * Problem Number: HW5
  7.  * Simple program that shows the number of possible combinations of individual items
  8.  * that can be selected from a single group
  9.  */
  10.  
  11. import java.math.BigInteger;
  12. import java.util.Scanner;
  13.  
  14. public class BigIntegerCombination {
  15.    
  16.    
  17.     //**********************************************
  18.    
  19.     private static void process(Scanner sc, String args[]) {
  20.         int n;
  21.         int k;
  22.        
  23.         System.out.println("Enter N: ");
  24.         n = sc.nextInt();  
  25.         System.out.println("Enter K: ");
  26.         k = sc.nextInt();
  27.         sc.nextLine();
  28.        
  29.        
  30.         System.out.println("C(n,k) is ");
  31.         System.out.println(getNumberOfCombinations(n,k).toString()+"\n<End Output>\n");
  32.     }
  33.    
  34.     private static BigInteger getNumberOfCombinations(int n, int k) {
  35.         BigInteger output;
  36.        
  37.         output = (factorial(n).divide( (factorial(k).multiply(factorial((n-k)))) ));
  38.        
  39.         return output;
  40.     }
  41.  
  42.     private static BigInteger factorial(int i) {
  43.         BigInteger factorial = new BigInteger("1");
  44.         BigInteger hold = new BigInteger("1");
  45.        
  46.         for(int j = 2 ; j <= i ; j++)
  47.         {
  48.             hold = new BigInteger(Integer.toString(j));
  49.             factorial = hold.multiply(factorial);  
  50.         }
  51.        
  52.         return factorial;
  53.     }
  54.  
  55.     //**********************************************
  56.    
  57.     private static boolean doThisAgain(Scanner sc, String prompt) {
  58.         System.out.print(prompt);
  59.         String doOver = sc.nextLine();
  60.         return doOver.equalsIgnoreCase("Y");
  61.     }
  62.    
  63.     //**********************************************
  64.    
  65.     public static void main(String args[]) {
  66.         final String TITLE = "CSC111 Project Template";
  67.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  68.        
  69.         System.out.println("Welcome to " + TITLE);
  70.         Scanner sc = new Scanner(System.in);
  71.         do {
  72.             process(sc, args);
  73.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  74.         sc.close();
  75.         System.out.println("Thank you for using " + TITLE);
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement