(IN JAVA) The formula for computing the number of ways of choosing r different t
ID: 3776714 • Letter: #
Question
(IN JAVA)
The formula for computing the number of ways of choosing r different things from a set of n things is the following:
C(n,r) = n! / r!(n-r)!
In this formula, the factorial function is represented by an exclamation point (!) and defined as the product.
n! = n * (n – 1) * (n – 2) * … * 1
Discover a recursive version of the C(n, r) formula, and write a recursive method that computes the value of the formula. Embed the method in a program and test it.
Explanation / Answer
class Factorial
{
static int factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return(n * factorial(n-1));
}
}
public static void main(String args[])
{
int i,n,fact=1;
Scanner s = new Scanner(System.in);
System.out.println("Enter a number:");
n=s.nextInt();
fact = factorial(n);
System.out.println("Factorial of "+n+" is: "+fact);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.