In the previous article, we have discussed Python Program for Minimum Perimeter of n Blocks

Given a grid of size N*M the task is to find the number of rectangles in the given grid in Python, Number of rectangles in a 4×4 grid, Number of rectangles formula, number of rectangles in a 3×3 grid, number of squares in mn grid, number of rectangles in the grid shown which are not squares is, count number of rectangles given points, number of rectangles in a grid leetcode, formula for number of squares in a grid, number of squares in m*n grid, number of rectangles in mxn grid, python program to find area of rectangle, you will get an idea.

**Examples:**

**Example1:**

**Input:**

Given N = 6 Given M = 4

**Output:**

Number of Rectangles in the grid of size { 6 * 4 } : 210

- Python Program to Print Rectangle Star Pattern
- Python Program to Print Hollow Rectangle Star Pattern
- Python Program to Find Sum of Geometric Progression Series

**Example2:**

**Input:**

Given N = 4 Given M = 2

**Output:**

Number of Rectangles in the grid of size { 4 * 2 } : 30

## Program to Find Number of Rectangles in N*M Grid in Python

Below are the ways to find the number of rectangles in the given N*M:

Let’s try to come up with a formula for the number of rectangles.

There is one rectangle in a grid of 1*1

There will be 2 + 1 = 3 rectangles if the grid is 2*1.

There will be 3 + 2 + 1 = 6 rectangles if the grid is 3*1

The formula for Number of Rectangles =

M(M+1)(N)(N+1)/4

### Method #1: Using Mathematical Formula (Static Input)

**Approach:**

- Give the number N as static input and store it in a variable.
- Give the number M as static input and store it in another variable.
- Create a function
**getCountRect()**which accepts given N, M grid sides as arguments and returns the number of rectangles. - Inside the
**getCountRect()**function Calculate the number of rectangles using the MatheMatical Formula**M(M+1)(N)(N+1)/4**and store it in a variable say reslt. - Return the reslt value.
- Inside the Main Function.
- Pass the given N, M as arguments to
**getCountRect**() function and store the result returned from the function in a variable say**countRect**. - Print the
**countRect**value. - The Exit of the Program.

**Below is the implementation:**

# Create a function getCountRect() which accepts given N, M grid sides # as arguments and returns the number of rectangles. def getCountRect(nVal, mVal): # Inside the getCountRect() function Calculate the number of rectangles # using the MatheMatical Formula M(M+1)(N)(N+1)/4 # and store it in a variable say reslt. reslt = (mVal * nVal * (nVal + 1) * (mVal + 1)) / 4 # Return the reslt value. return reslt # Inside the Main Function. # Give the number N as static input and store it in a variable. nVal = 6 # Give the number M as static input and store it in another variable. mVal = 4 # Pass the given N, M as arguments to getCountRect() function # and store the result returned from the function in a variable say countRect. countRect = int(getCountRect(nVal, mVal)) # Print the countRect value. print( 'Number of Rectangles in the grid of size {', nVal, '*', mVal, '} :', countRect)

#include <iostream> using namespace std; int getCountRect ( int nVal, int mVal ) { int reslt = ( mVal * nVal * ( nVal + 1 ) * ( mVal + 1 ) ) / 4; return reslt; } int main() { int nVal = 6; int mVal = 4; int countRect = ( int ) getCountRect ( nVal, mVal ); cout << "Number of Rectangles in the grid of size {" << nVal << '*' << mVal << " } is: " << countRect << endl; return 0; }

**Output:**

Number of Rectangles in the grid of size { 6 * 4 } : 210

### Method #2: Using Mathematical Formula (User Input)

**Approach:**

- Give the number N as user input using the int(input()) function and store it in a variable.
- Give the number M as user input using the int(input()) function and store it in another variable.
- Create a function
**getCountRect()**which accepts given N, M grid sides as arguments and returns the number of rectangles. - Inside the
**getCountRect()**function Calculate the number of rectangles using the MatheMatical Formula**M(M+1)(N)(N+1)/4**and store it in a variable say reslt. - Return the reslt value.
- Inside the Main Function.
- Pass the given N, M as arguments to
**getCountRect**() function and store the result returned from the function in a variable say**countRect**. - Print the
**countRect**value. - The Exit of the Program.

**Below is the implementation:**

# Create a function getCountRect() which accepts given N, M grid sides # as arguments and returns the number of rectangles. def getCountRect(nVal, mVal): # Inside the getCountRect() function Calculate the number of rectangles # using the MatheMatical Formula M(M+1)(N)(N+1)/4 # and store it in a variable say reslt. reslt = (mVal * nVal * (nVal + 1) * (mVal + 1)) / 4 # Return the reslt value. return reslt # Inside the Main Function. # Give the number N as user input using the int(input()) function and store it in a variable. nVal = int(input('Enter Some Random N value = ')) # Give the number M as static input and store it in another variable. mVal = int(input('Enter Some Random M value = ')) # Pass the given N, M as arguments to getCountRect() function # and store the result returned from the function in a variable say countRect. countRect = int(getCountRect(nVal, mVal)) # Print the countRect value. print( 'Number of Rectangles in the grid of size {', nVal, '*', mVal, '} :', countRect)

**Output:**

Enter Some Random N value = 4 Enter Some Random M value = 2 Number of Rectangles in the grid of size { 4 * 2 } : 30

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.

**Analyze yourself:**

- Python program to read number n and compute n*nn*nnn?
- How to find number of rectangles in a grid?
- How to calculate number of rectangles in a grid?
- Python program to find area of a rectangle?
- Finding number of rectangles in a grid?
- Python program to find area of rectangle using class?

**Related Posts On:**