Given three numbers , , . Find Number of squares of dimension required to cover rectangle.

**Note**:

- It’s allowed to cover the surface larger than the rectangle, but the rectangle has to be covered.
- It’s not allowed to break a square.
- The sides of squares should be parallel to the sides of the rectangle.

**Examples**:

Input: N = 6, M = 6, a = 4 Output: 4 Input: N = 2, M = 3, a = 1 Output: 6

**Approach:** An efficient approach is to make an observation and find a formula. The constraint that edges of each square must be parallel to the edges of the rectangle that allows to analyze X and Y axes separately, that is, how many squares of length ‘a’ are needed to cover squares of length ‘m’ and ‘n’ and take the product of these two quantities. The number of small squares of side length ‘a’ required to cover ‘m’ sized square are ceil(m/a). Simillary, number of ‘a’ sized squares required to cover ‘n’ sized square are ceil(n/a).

So, the answer will be ceil(m/a)*ceil(n/a).

Below is the implementation of the above approach:

## C++

`// CPP program to find number of squares ` `// of a*a required to cover n*m rectangle ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find number of squares ` `// of a*a required to cover n*m rectangle ` `int` `Squares(` `int` `n, ` `int` `m, ` `int` `a) ` `{ ` ` ` `return` `((m + a - 1) / a) * ((n + a - 1) / a); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6, m = 6, a = 4; ` ` ` ` ` `// function call ` ` ` `cout << Squares(n, m, a); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of squares ` `// of a*a required to cover n*m rectangle ` `import` `java.util.*; ` ` ` `class` `solution ` `{ ` ` ` `// function to find a number of squares ` `// of a*a required to cover n*m rectangle ` `static` `int` `Squares(` `int` `n, ` `int` `m, ` `int` `a) ` `{ ` ` ` ` ` `return` `((m + a - ` `1` `) / a) * ((n + a - ` `1` `) / a); ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String arr[]) ` `{ ` ` ` `int` `n = ` `6` `, m = ` `6` `, a = ` `4` `; ` ` ` ` ` `// function call ` ` ` `System.out.println(Squares(n, m, a)); ` ` ` `} ` ` ` `} ` `//This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find number ` `# of squares of a*a required to ` `# cover n*m rectangle ` ` ` `# function to find number of ` `# squares of a*a required to ` `# cover n*m rectangle ` `def` `Squares(n, m, a): ` ` ` `return` `(((m ` `+` `a ` `-` `1` `) ` `/` `/` `a) ` `*` ` ` `((n ` `+` `a ` `-` `1` `) ` `/` `/` `a)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `6` ` ` `m ` `=` `6` ` ` `a ` `=` `4` ` ` ` ` `# function call ` ` ` `print` `(Squares(n, m, a)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// CSHARP program to find number of squares ` `// of a*a required to cover n*m rectangle ` ` ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to find a number of squares ` ` ` `// of a*a required to cover n*m rectangle ` ` ` `static` `int` `Squares(` `int` `n, ` `int` `m, ` `int` `a) ` ` ` `{ ` ` ` ` ` `return` `((m + a - 1) / a) * ((n + a - 1) / a); ` ` ` ` ` `} ` ` ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 6, m = 6, a = 4; ` ` ` ` ` `// function call ` ` ` `Console.WriteLine(Squares(n, m, a)); ` ` ` `} ` ` ` `// This code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number of squares ` `// of a*a required to cover n*m rectangle ` ` ` `// function to find number of squares ` `// of a*a required to cover n*m rectangle ` `function` `Squares(` `$n` `, ` `$m` `, ` `$a` `) ` `{ ` ` ` `return` `((int)((` `$m` `+ ` `$a` `- 1) / ` `$a` `)) * ` ` ` `((int)((` `$n` `+ ` `$a` `- 1) / ` `$a` `)); ` `} ` ` ` `// Driver code ` `$n` `= 6; ` `$m` `= 6; ` `$a` `= 4; ` ` ` `// function call ` `echo` `Squares(` `$n` `, ` `$m` `, ` `$a` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Minimum squares to cover a rectangle
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Count squares with odd side length in Chessboard
- Minimum number of points required to cover all blocks of a 2-D grid
- Maximum number of tiles required to cover the floor of given size using 2x1 size tiles
- Number of squares of maximum area in a rectangle
- Count number of squares in a rectangle
- Minimum time required to cover a Binary Array
- Ratio of area of a rectangle with the rectangle inscribed in it
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Find the Side of the smallest Square that can contain given 4 Big Squares
- Find the side of the squares which are inclined diagonally and lined in a row
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Minimum squares to evenly cut a rectangle
- Length of diagonals of a Rhombus using length of Side and vertex Angle
- Minimize number of cuts required to break N length stick into N unit length sticks
- Count number of ways to cover a distance | Set 2
- Count minimum number of fountains to be activated to cover the entire garden
- Largest number from the longest set of anagrams possible from all perfect squares of length K

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.