Menu

[Solved] Write Java Program Output Number Distinct Ways Pick K Objects Set N Objects N K Positive I Q37232914

Write a Java program that will output the number of distinctways in which you can pick k objects out of a set ofn objects (both n and k should be positiveintegers). This number is given by the following formula:

C(n, k) = n! / (k! * (n – k)!)

Your program should use 2 value-returning methods:

1. The first method should be called factorialand should return n! The factorial of a positiveinteger n is the product of all integers from 1 to n. Youcan express the factorial of a positive integer n (inmathematics this is denoted by n!) using thefollowing formula:
n! = 1 * 2 * 3 * … * (n – 1) * n

The following specifications describe a method that computesfactorials:

Input parameter: int n (where n is the numberwhose factorial is to be computed)
Return: the factorial of n, which is computed using theformula for n!

2. The second method should be calledcombinations and should return n! / (k! *(n – k)!). This method should callfactorial.
The following specifications describe a method that computescombinations:

Input parameters: int n, k
Return: n choose k, which is computed using theformula for C(n, k).

Test your program for different values of n andk (valid range for both: 1 to 20)in a sentinel-controlled loop (“Continue?”). Checkfor errors in input (in main, NOT inmethods!).
Store the output to a file named output.txt aswell.

Expert Answer


Answer to Write a Java program that will output the number of distinct ways in which you can pick k objects out of a set of n obje… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *