530,579 views
27 votes
27 votes
Write a recursive method called permut that accepts two integers n and r as parameters and returns the number of unique permutations of r items from a group of n items. For given values of n and r, this value P(n, r) can be computed as follows:

n!/(n - r)!
For example , permut (7, 4) should return 840.

User Ssemichev
by
3.1k points

1 Answer

14 votes
14 votes

Answer:

Following are the code to the given question:

public class Main//defining a class Main

{

static int permut(int n, int r)//defining a method permut that holds two variable

{

return fact(n)/fact(n-r);//use return keyword to return calcuate value

}

static int fact(int n)//defining a fact method as recursive to calculate factorials

{

return n==0?1:n*fact(n-1);//calling the method recursively

}

public static void main(String[] abs)//main function

{

//int n=7,r=4;//defining integer variable

System.out.println(permut(7,4));//use print method to call permut method and print its values

}

}

Output:

840

Explanation:

Following is the explanation for the above code.

  • Defining a class Main.
  • Inside the class two methods, "permut and fact" were defined, in which the "permut" accepts two values for calculating its permutated value, and the fact is used for calculates factorial values recursively.
  • At the last, the main method is declared, which uses the print method to call "permut" and prints its return values.
User Volpato
by
2.9k points