Given two integers **N** and **M**, where **N** straight lines are parallel to the **X-axis** and **M** straight lines are parallel to **Y-axis**, the task is to calculate the number of rectangles that can be formed by these lines.**Examples:**

Input:N = 3, M = 6Output:45Explanation:

There are total 45 rectangles possible with 3 lines parallel to x axis and 6 lines parallel to y axis.Input:N = 2, M = 4Output:6Explanation:

There are total 6 rectangles possible with 2 lines parallel to x axis and 4 lines parallel to y axis.

**Approach:**

To solve the problem mentioned above we need to observe that a rectangle is formed by 4 straight lines in which opposite sides are parallel and the angle between any two sides is 90. Hence, for every rectangle, two sides need to be parallel to **X-axis** and the other two sides need to be parallel to Y-axis.

- Number of ways to select two lines parallel to X axis =
^{N}C_{2}_{ }and the Number of ways to select two lines parallel to Y axis =^{M}C_{2}_{ }. - So the total number of rectangles =
=^{N}C_{2 * }^{M}C_{2 }**[ N * (N – 1) / 2 ] * [ M * (M – 1) / 2 ]**

Below is implementation of above approach:

## C++

`// C++ Program to count number of ` `// rectangles formed by N lines ` `// parallel to X axis M lines ` `// parallel to Y axis ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate ` `// number of rectangles ` `int` `count_rectangles(` `int` `N, ` `int` `M) ` `{ ` ` ` `// Total number of ways to ` ` ` `// select two lines ` ` ` `// parallel to X axis ` ` ` `int` `p_x = (N * (N - 1)) / 2; ` ` ` ` ` `// Total number of ways ` ` ` `// to select two lines ` ` ` `// parallel to Y axis ` ` ` `int` `p_y = (M * (M - 1)) / 2; ` ` ` ` ` `// Total number of rectangles ` ` ` `return` `p_x * p_y; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 3; ` ` ` ` ` `int` `M = 6; ` ` ` ` ` `cout << count_rectangles(N, M); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to count number of ` `// rectangles formed by N lines ` `// parallel to X axis M lines ` `// parallel to Y axis ` `class` `GFG{ ` ` ` `// Function to calculate ` `// number of rectangles ` `static` `int` `count_rectangles(` `int` `N, ` `int` `M) ` `{ ` ` ` `// Total number of ways to ` ` ` `// select two lines ` ` ` `// parallel to X axis ` ` ` `int` `p_x = (N * (N - ` `1` `)) / ` `2` `; ` ` ` ` ` `// Total number of ways ` ` ` `// to select two lines ` ` ` `// parallel to Y axis ` ` ` `int` `p_y = (M * (M - ` `1` `)) / ` `2` `; ` ` ` ` ` `// Total number of rectangles ` ` ` `return` `p_x * p_y; ` `} ` ` ` `// Driver Program ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `int` `M = ` `6` `; ` ` ` ` ` `System.out.print(count_rectangles(N, M)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number of rectangles ` `# formed by N lines parallel to X axis ` `# and M lines parallel to Y axis ` `def` `count_rectangles(N, M): ` ` ` ` ` `# Total number of ways to select ` ` ` `# two lines parallel to X axis ` ` ` `p_x ` `=` `(N ` `*` `(N ` `-` `1` `)) ` `/` `/` `2` ` ` ` ` `# Total number of ways to select ` ` ` `# two lines parallel to Y axis ` ` ` `p_y ` `=` `(M ` `*` `(M ` `-` `1` `)) ` `/` `/` `2` ` ` ` ` `# Total number of rectangles ` ` ` `return` `p_x ` `*` `p_y ` ` ` `# Driver code ` `N ` `=` `3` `M ` `=` `6` ` ` `print` `(count_rectangles(N, M)) ` ` ` `# This code is contributed by himanshu77 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count number of ` `// rectangles formed by N lines ` `// parallel to X axis M lines ` `// parallel to Y axis ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to calculate ` `// number of rectangles ` `static` `int` `count_rectangles(` `int` `N, ` `int` `M) ` `{ ` ` ` `// Total number of ways to ` ` ` `// select two lines ` ` ` `// parallel to X axis ` ` ` `int` `p_x = (N * (N - 1)) / 2; ` ` ` ` ` `// Total number of ways ` ` ` `// to select two lines ` ` ` `// parallel to Y axis ` ` ` `int` `p_y = (M * (M - 1)) / 2; ` ` ` ` ` `// Total number of rectangles ` ` ` `return` `p_x * p_y; ` `} ` ` ` `// Driver Program ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `M = 6; ` ` ` ` ` `Console.Write(count_rectangles(N, M)); ` `} ` `} ` ` ` `// This code is contributed by Code_mech` |

*chevron_right*

*filter_none*

**Output:**

45

**Time Complexity: ** O(1)

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:

- Missing vertex among N axis-parallel rectangles
- Number of lines from given N points not parallel to X or Y axis
- Count of different straight lines with total n points with m collinear
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Program to check if the points are parallel to X axis or Y axis
- Check if three straight lines are concurrent or not
- Check if given two straight lines are identical or not
- Check whether two straight lines are orthogonal or not
- Distance between two parallel lines
- Maximum points of intersections possible among X circles and Y straight lines
- Find whether only two parallel lines contain all coordinates points or not
- Puzzle | Connect 9 circles each arranged at center of a Matrix using 3 straight lines
- Maximum number of region in which N non-parallel lines can divide a plane
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Find the equation of plane which passes through two points and parallel to a given axis
- Count Distinct Rectangles in N*N Chessboard
- Count of distinct rectangles inscribed in an equilateral triangle
- Count the number of rectangles such that ratio of sides lies in the range [a,b]
- Count number of pairs of lines intersecting at a Point
- Count of intersections of M line segments with N vertical lines in XY plane

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.