Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The method should take one integer parameter and return the integer factorial. W

ID: 3642956 • Letter: T

Question

The method should take one integer parameter and return the integer factorial.
Write a main program that reads in two integers, n and k, and displays the number of possible combinations of n things taken k at a time. The number of combinations can be calculated by
combinations= n!/(n-k)! k!
Your program must use your factorial method. The program should ask the user for n and k and display the number of combinations. The program should continue asking the user for n and k until a value of zero is entered for n.

Example input and output
5 3
There are 10 combinationations of 5 things taken 3 at a time.
10 4
Thare are 210 combinations of 10 things taken 4 at a time.

Explanation / Answer

import java.util.Scanner;

public class CombinUsingFactorial {
    public static int factorial(int n) {
        if (n == 0)
            return 1;
        int fact = 1;
        while (n > 1)
            fact *= n--;
        return fact;
    }
  
    public static int Combinator(int n, int k) {
        return factorial(n) / factorial(n - k) / factorial(k);
    }
  
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = 0;
        int k = 0;
      
        while (true) {
            n = in.nextInt(); // max n is 12 because 13! is about 6 billion
            k = in.nextInt();
            if (n == 0 && k == 0)
                break;
            System.out.println("Thare are " + Combinator(n, k) +
                " combinations of " + n + " things taken " + k + " at a time.");
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote