{"id":22983,"date":"2021-09-29T20:14:40","date_gmt":"2021-09-29T14:44:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=22983"},"modified":"2021-11-22T18:35:35","modified_gmt":"2021-11-22T13:05:35","slug":"python-program-to-find-number-of-rectangles-in-nm-grid","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-number-of-rectangles-in-nm-grid\/","title":{"rendered":"Python Program to Find Number of Rectangles in N*M Grid"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Minimum Perimeter of n Blocks<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the number of rectangles in the given N*M:<\/p>\n Let’s try to come up with a formula for the number of rectangles.<\/p>\n There is one rectangle in a grid of 1*1<\/p>\n There will be 2 + 1 = 3 rectangles if the grid is 2*1.<\/p>\n There will be 3 + 2 + 1 = 6 rectangles if the grid is 3*1<\/p>\n The formula for Number of Rectangles = M(M+1)(N)(N+1)\/4<\/strong><\/p><\/blockquote>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more Example Python Programs<\/a> with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.<\/p>\n 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 …<\/p>\n
\nGiven a grid of size N*M the task is to find the number of rectangles in the given grid in Python.<\/p>\nGiven N = 6\r\nGiven M = 4<\/pre>\n
Number of Rectangles in the grid of size { 6 * 4 } : 210<\/pre>\n
Given N = 4\r\nGiven M = 2<\/pre>\n
Number of Rectangles in the grid of size { 4 * 2 } : 30<\/pre>\n
Program to Find Number of Rectangles in N*M Grid in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Create a function getCountRect() which accepts given N, M grid sides\r\n# as arguments and returns the number of rectangles.\r\n\r\n\r\ndef getCountRect(nVal, mVal):\r\n # Inside the getCountRect()\u00a0function Calculate the number of rectangles\r\n # using the MatheMatical Formula M(M+1)(N)(N+1)\/4\u00a0\r\n # and store it in a variable say reslt.\r\n reslt = (mVal * nVal * (nVal + 1) * (mVal + 1)) \/ 4\r\n # Return the reslt value.\r\n return reslt\r\n\r\n\r\n# Inside the Main Function.\r\n# Give the number N as static input and store it in a variable.\r\nnVal = 6\r\n# Give the number M as static input and store it in another variable.\r\nmVal = 4\r\n# Pass the given N, M as arguments to getCountRect() function\r\n# and store the result returned from the function in a variable say countRect.\r\ncountRect = int(getCountRect(nVal, mVal))\r\n# Print the countRect value.\r\nprint(\r\n 'Number of Rectangles in the grid of size {', nVal, '*', mVal, '} :', countRect)\r\n<\/pre>\n
#include <iostream>\r\n\r\nusing namespace std;\r\nint getCountRect ( int nVal, int mVal ) {\r\n int reslt = ( mVal * nVal * ( nVal + 1 ) * ( mVal + 1 ) ) \/ 4;\r\n return reslt;\r\n}\r\nint main() {\r\n int nVal = 6;\r\n int mVal = 4;\r\n int countRect = ( int ) getCountRect ( nVal, mVal );\r\n cout << \"Number of Rectangles in the grid of size {\" << nVal << '*' << mVal << \" } is: \" << countRect << endl;\r\n return 0;\r\n}\r\n\r\n<\/pre>\n
Number of Rectangles in the grid of size { 6 * 4 } : 210<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Create a function getCountRect() which accepts given N, M grid sides\r\n# as arguments and returns the number of rectangles.\r\n\r\n\r\ndef getCountRect(nVal, mVal):\r\n # Inside the getCountRect()\u00a0function Calculate the number of rectangles\r\n # using the MatheMatical Formula M(M+1)(N)(N+1)\/4\u00a0\r\n # and store it in a variable say reslt.\r\n reslt = (mVal * nVal * (nVal + 1) * (mVal + 1)) \/ 4\r\n # Return the reslt value.\r\n return reslt\r\n\r\n\r\n# Inside the Main Function.\r\n# Give the number N as user input using the int(input()) function and store it in a variable.\r\nnVal = int(input('Enter Some Random N value = '))\r\n# Give the number M as static input and store it in another variable.\r\nmVal = int(input('Enter Some Random M value = '))\r\n# Pass the given N, M as arguments to getCountRect() function\r\n# and store the result returned from the function in a variable say countRect.\r\ncountRect = int(getCountRect(nVal, mVal))\r\n# Print the countRect value.\r\nprint(\r\n 'Number of Rectangles in the grid of size {', nVal, '*', mVal, '} :', countRect)\r\n<\/pre>\n
Enter Some Random N value = 4\r\nEnter Some Random M value = 2\r\nNumber of Rectangles in the grid of size { 4 * 2 } : 30<\/pre>\n
\n