Advertisement
philcrafts

BigInteger Adam/Phil

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