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.
Examples:
Example1:
Input:
Given N = 6 Given M = 4
Output:
Number of Rectangles in the grid of size { 6 * 4 } : 210Example2:
Input:
Given N = 4 Given M = 2
Output:
Number of Rectangles in the grid of size { 4 * 2 } : 30Program 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 } : 210Method #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 } : 30Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.
