इस लेख में, हम समझेंगे कि n और r मानों का उपयोग करके संयोजन की गणना कैसे करें। nCr की गणना सूत्र का उपयोग करके की जाती है -
(factorial of n) / (factorial of (n-r))
नीचे उसी का एक प्रदर्शन है -
इनपुट
मान लीजिए हमारा इनपुट है -
Value of n : 6 Value of r : 4
आउटपुट
वांछित आउटपुट होगा -
The nCr value is : 15
एल्गोरिदम
Step 1 - START Step 2 - Declare two integer values namely n and r. Step 3 - Read the required values from the user/ define the values Step 4 - Define two functions, one function to calculate the factorial of n and (n-r) and other to compute the formula : (factorial of n) / (factorial of (n-r)) and store the result. Step 5 - Display the result Step 6 - Stop
उदाहरण 1
यहां, उपयोगकर्ता द्वारा एक प्रॉम्प्ट के आधार पर इनपुट दर्ज किया जा रहा है। आप इस उदाहरण को हमारे कोडिंग ग्राउंड टूल में लाइव आज़मा सकते हैं
।
import java.util.*;
public class Combination {
static int Compute_nCr(int n, int r){
return my_factorial(n) / (my_factorial(r) *
my_factorial(n - r));
}
static int my_factorial(int n){
int i, my_result;
my_result = 1;
for (i = 2; i <= n; i++)
my_result = my_result * i;
return my_result;
}
public static void main(String[] args){
int n,r;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the value of n : ");
n = my_scanner.nextInt();
System.out.print("Enter the value of r : ");
r = my_scanner.nextInt();
System.out.println("The combination value for the given input is = "+Compute_nCr(n, r));
}
} आउटपुट
Required packages have been imported A reader object has been defined Enter the value of n : 6 Enter the value of r : 4 The combination value for the given input is = 15
उदाहरण 2
यहां, पूर्णांक को पहले परिभाषित किया गया है, और इसके मान को एक्सेस किया जाता है और कंसोल पर प्रदर्शित किया जाता है।
public class Combination {
static int Compute_nCr(int n, int r){
return my_factorial(n) / (my_factorial(r) *
my_factorial(n - r));
}
static int my_factorial(int n){
int i, my_result;
my_result = 1;
for (i = 2; i <= n; i++)
my_result = my_result * i;
return my_result;
}
public static void main(String[] args){
int n,r;
n = 6 ;
r = 4 ;
System.out.println("The n and r values are defined as " +n + " and " +r);
System.out.println("The combination value for the given input is = "+Compute_nCr(n, r));
}
} आउटपुट
The n and r values are defined as 6 and 4 The combination value for the given input is = 15है