Menu

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

Write a Java program that will output the number of distinctways in which you can pick k objects out of a set of n objects(both n and k should be positive integers). This number is given bythe 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 positive integer n is theproduct of all integers from 1 to n. You can express thefactorial of a positive integer n (in mathematics this isdenoted by n!) using the following formula:n! = 1 * 2 * 3 * … * (n – 1) * n

The following specifications describe a method that computesfactorials:

Input parameter: int n (where n is the number whosefactorial 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.

SAMPLE OUTPUT: Do you want to start? y Enter 2 positive integers (n, k): 12 34 ERROR! Both should be (1 - 10) and k < n. Reen

SAMPLE OUTPUT: Do you want to start? y Enter 2 positive integers (n, k): 12 34 ERROR! Both should be (1 – 10) and k < n. Reenter: 7 6 ERROR! Both should be (1 – 10) and k< n. Reenter: 3 8 ERROR! Both should be (1 – 10) and k< n. Reenter: 8 3 8 , 3) = 56 ー Do you want to continue? y Enter 2 positive integers (n, k): 6 4 C (6,4)15 Do you want to continue? y Enter 2 positive integers(n, k): 5 4 C (5 , 4)5 Do you want to continue? n Show transcribed image text SAMPLE OUTPUT: Do you want to start? y Enter 2 positive integers (n, k): 12 34 ERROR! Both should be (1 – 10) and k

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 *