# Total ways of choosing X men and Y women from a total of M men and W women

Given four integers **X**, **Y**, **M** and **W**. The task is to find the number of ways to choose **X** men and **Y** women from total **M** men and **W** women.

**Examples:**

Input:X = 1, Y = 2, M = 1, W = 3

Output:3

Way 1: Choose the only man and 1^{st}and 2^{nd}women.

Way 2: Choose the only man and 2^{nd}and 3^{rd}women.

Way 3: Choose the only man and 1^{st}and 3^{rd}women.

Input:X = 4, Y = 3, M = 6, W = 5

Output:150

**Approach:** The total number of ways of choosing **X** men from a total of **M** men is ** ^{M}C_{X}** and the total number of ways of choosing

**Y**women from

**W**women is

**. Hence, the total number of combined ways will be**

^{W}C_{Y}**.**

^{M}C_{X}*^{W}C_{Y}Below is the implementation of the above approach:

## C++

`// C++ implementataion of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// value of ncr effectively ` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` ` ` `// Initialize the answer ` ` ` `int` `ans = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= r; i += 1) { ` ` ` ` ` `// Divide simultaneously by ` ` ` `// i to avoid overflow ` ` ` `ans *= (n - r + i); ` ` ` `ans /= i; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Function to return the count of required ways ` `int` `totalWays(` `int` `X, ` `int` `Y, ` `int` `M, ` `int` `W) ` `{ ` ` ` `return` `(ncr(M, X) * ncr(W, Y)); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `X = 4, Y = 3, M = 6, W = 5; ` ` ` ` ` `cout << totalWays(X, Y, M, W); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA implementataion of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the ` ` ` `// value of ncr effectively ` ` ` `static` `int` `ncr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` ` ` `// Initialize the answer ` ` ` `int` `ans = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= r; i += ` `1` `) ` ` ` `{ ` ` ` ` ` `// Divide simultaneously by ` ` ` `// i to avoid overflow ` ` ` `ans *= (n - r + i); ` ` ` `ans /= i; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Function to return the count of required ways ` ` ` `static` `int` `totalWays(` `int` `X, ` `int` `Y, ` `int` `M, ` `int` `W) ` ` ` `{ ` ` ` `return` `(ncr(M, X) * ncr(W, Y)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `X = ` `4` `, Y = ` `3` `, M = ` `6` `, W = ` `5` `; ` ` ` ` ` `System.out.println(totalWays(X, Y, M, W)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit_23 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementataion of the approach ` ` ` `# Function to return the ` `# value of ncr effectively ` `def` `ncr(n, r): ` ` ` `# Initialize the answer ` ` ` `ans ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `1` `,r` `+` `1` `): ` ` ` ` ` `# Divide simultaneously by ` ` ` `# i to avoid overflow ` ` ` `ans ` `*` `=` `(n ` `-` `r ` `+` `i) ` ` ` `ans ` `/` `/` `=` `i ` ` ` `return` `ans ` ` ` `# Function to return the count of required ways ` `def` `totalWays(X, Y, M, W): ` ` ` ` ` `return` `(ncr(M, X) ` `*` `ncr(W, Y)) ` ` ` `X ` `=` `4` `Y ` `=` `3` `M ` `=` `6` `W ` `=` `5` ` ` `print` `(totalWays(X, Y, M, W)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementataion of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the ` ` ` `// value of ncr effectively ` ` ` `static` `int` `ncr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` ` ` `// Initialize the answer ` ` ` `int` `ans = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= r; i += 1) ` ` ` `{ ` ` ` ` ` `// Divide simultaneously by ` ` ` `// i to avoid overflow ` ` ` `ans *= (n - r + i); ` ` ` `ans /= i; ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Function to return the count of required ways ` ` ` `static` `int` `totalWays(` `int` `X, ` `int` `Y, ` `int` `M, ` `int` `W) ` ` ` `{ ` ` ` `return` `(ncr(M, X) * ncr(W, Y)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `X = 4, Y = 3, M = 6, W = 5; ` ` ` ` ` `Console.WriteLine(totalWays(X, Y, M, W)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

150

## Recommended Posts:

- Ways of selecting men and women from a group to make a team
- Total number of ways to place X and Y at n places such that no two X are together
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Number of ways to get even sum by choosing three numbers from 1 to N
- Count ways of choosing a pair with maximum difference
- Number of ways of choosing K equal substrings of any length for every query
- Total no of 1's in numbers
- Find the total Number of Digits in (N!)N
- Count total divisors of A or B in a given range
- Count total set bits in all numbers from 1 to n | Set 2
- Count total number of digits from 1 to n
- Total number of different staircase that can made from N boxes
- Total number of subsets in which the product of the elements is even
- Total numbers with no repeated digits in a range
- Count of triangles with total n points with m collinear

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.