Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name:Philip Crafts/Adam Tiago
- * Date:10/27/2017
- * Course Number:CSC-220
- * Course Name:Data Structure
- * Problem Number: HW5
- * Email:philroycrafts@gmail.com
- * Simple program that shows the number of possible combinations of individual items
- * that can be selected from a single group
- */
- import java.math.BigInteger;
- import java.util.Scanner;
- public class BigIntegerCombination {
- //**********************************************
- private static void process(Scanner sc, String args[]) {
- int n;
- int k;
- System.out.println("Enter N: ");
- n = sc.nextInt();
- System.out.println("Enter K: ");
- k = sc.nextInt();
- sc.nextLine();
- System.out.println("C(n,k) is ");
- System.out.println(getNumberOfCombinations(n,k).toString()+"\n<End Output>\n");
- }
- private static BigInteger getNumberOfCombinations(int n, int k) {
- BigInteger output;
- output = (factorial(n).divide( (factorial(k).multiply(factorial((n-k)))) ));
- return output;
- }
- private static BigInteger factorial(int i) {
- BigInteger factorial = new BigInteger("1");
- BigInteger hold = new BigInteger("1");
- for(int j = 2 ; j <= i ; j++)
- {
- hold = new BigInteger(Integer.toString(j));
- factorial = hold.multiply(factorial);
- }
- return factorial;
- }
- //**********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- //**********************************************
- public static void main(String args[]) {
- final String TITLE = "CSC111 Project Template";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- process(sc, args);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement