Given four integers **X**, **Y**, **M** and **N**. The task is to find the number of ways to form a word by choosing **X** number of vowels and **Y** number of consonants from total numbers of **M** vowels and **N** consonants.

**Examples:**

Input :X = 2, Y = 2, M = 3, N = 3

Output :216

The total number of ways of choosing 2 vowels from a total number of 3 vowels is i.e 3

The total number of ways of choosing 2 consonants from a total number of 3 consonants is i.e 3.

The total number of ways of selecting 2 consonants from 3 and 2 vowels from 3 is * = 9

The total number of ways of arranging 4 letters among themselves = 4! = 24

Hence, the required number of ways = 24 * 9 = 216

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

Output :36

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach :**

- The total number of ways of choosing X vowels from a total number of M vowels is
- The total number of ways of choosing Y consonants from a total number of N consonants is
- The total number of ways of selecting Y consonants from N and X vowels from M is *
- The total number of ways of arranging (X+Y) letters among themselves = (X+Y)!
- Hence, the required number of ways = (X+Y)! * *

Below is the implementation of the above approach:

## C++

`// CPP program to find the number of words ` `// of X vowels and Y consonants can be ` `// formed from M vowels and N consonants ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to returns factorial of n ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` `} ` ` ` `// Function to find nCr ` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `return` `fact(n) / (fact(r) * fact(n - r)); ` `} ` ` ` `// Function to find the number of words ` `// of X vowels and Y consonants can be ` `// formed from M vowels and N consonants ` `int` `NumberOfWays(` `int` `X, ` `int` `Y, ` `int` `M, ` `int` `N) ` `{ ` ` ` `return` `fact(X + Y) * nCr(M, X) * nCr(N, Y); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 2, Y = 2, M = 3, N = 3; ` ` ` ` ` `// Function call ` ` ` `cout << NumberOfWays(X, Y, M, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of words ` `// of X vowels and Y consonants can be ` `// formed from M vowels and N consonants ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to returns factorial of n ` ` ` `static` `int` `fact(` `int` `n) ` ` ` `{ ` ` ` `int` `res = ` `1` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to find nCr ` ` ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `return` `fact(n) / (fact(r) * ` ` ` `fact(n - r)); ` ` ` `} ` ` ` ` ` `// Function to find the number of words ` ` ` `// of X vowels and Y consonants can be ` ` ` `// formed from M vowels and N consonants ` ` ` `static` `int` `NumberOfWays(` `int` `X, ` `int` `Y, ` ` ` `int` `M, ` `int` `N) ` ` ` `{ ` ` ` `return` `fact(X + Y) * nCr(M, X) * ` ` ` `nCr(N, Y); ` ` ` `} ` ` ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `throws` `java.lang.Exception ` ` ` `{ ` ` ` `int` `X = ` `2` `, Y = ` `2` `, M = ` `3` `, N = ` `3` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(NumberOfWays(X, Y, M, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nidhiva ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the number of words ` `# of X vowels and Y consonants can be ` `# formed from M vowels and N consonants ` ` ` `# Function to returns factorial of n ` `def` `fact(n): ` ` ` `res ` `=` `1` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `, ` `1` `): ` ` ` `res ` `=` `res ` `*` `i ` ` ` `return` `res ` ` ` `# Function to find nCr ` `def` `nCr(n, r): ` ` ` `return` `fact(n) ` `/` `/` `(fact(r) ` `*` `fact(n ` `-` `r)) ` ` ` `# Function to find the number of words ` `# of X vowels and Y consonants can be ` `# formed from M vowels and N consonants ` `def` `NumberOfWays(X, Y, M, N): ` ` ` `return` `fact(X ` `+` `Y) ` `*` `nCr(M, X) ` `*` `nCr(N, Y) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `X ` `=` `2` ` ` `Y ` `=` `2` ` ` `M ` `=` `3` ` ` `N ` `=` `3` ` ` ` ` `# Function call ` ` ` `print` `(NumberOfWays(X, Y, M, N)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of words ` `// of X vowels and Y consonants can be ` `// formed from M vowels and N consonants ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to returns factorial of n ` ` ` `static` `int` `fact(` `int` `n) ` ` ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to find nCr ` ` ` `static` `int` `nCr(` `int` `n, ` `int` `r) ` ` ` `{ ` ` ` `return` `fact(n) / (fact(r) * ` ` ` `fact(n - r)); ` ` ` `} ` ` ` ` ` `// Function to find the number of words ` ` ` `// of X vowels and Y consonants can be ` ` ` `// formed from M vowels and N consonants ` ` ` `static` `int` `NumberOfWays(` `int` `X, ` `int` `Y, ` ` ` `int` `M, ` `int` `N) ` ` ` `{ ` ` ` `return` `fact(X + Y) * nCr(M, X) * ` ` ` `nCr(N, Y); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `X = 2, Y = 2, M = 3, N = 3; ` ` ` ` ` `// Function call ` ` ` `Console.WriteLine(NumberOfWays(X, Y, M, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

216

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Replace consonants with next immediate consonants alphabetically in a String
- Encrypt string with product of number of vowels and consonants in substring of size k
- Check whether all the substrings have number of vowels atleast as that of consonants
- Arrangement of words without changing the relative position of vowel and consonants
- Arrange consonants and vowels nodes in a linked list
- Count strings with consonants and vowels at alternate position
- Longest subsequence consisting of alternate vowels and consonants
- Longest Substring having equal count of Vowels and Consonants
- Modify string by removing vowels in between two consonants
- Replace all consonants with nearest vowels in a string
- Modify the string by swapping continuous vowels or consonants
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Find maximum number that can be formed using digits of a given number
- Find the largest number that can be formed by changing at most K digits
- Largest even number that can be formed by any number of swaps
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Find if a molecule can be formed from 3 atoms using their valence numbers
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Number of triangles that can be formed with given N points

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.