Minimum number of square tiles required to fill the rectangular floor

Given a rectanglular floor of (M X N) meters is to be paved with square tiles of (s X s). The task is to find the minimum number of tiles required to pave the rectangular floor.

Constraints:

  1. It’s allowed to cover the surface larger than the floor, but the floor has to be covered.
  2. It’s not allowed to break the tiles.
  3. The sides of tiles should be parallel to the sides of the floor.

Examples:

Input: 2 1 2
Output: 1
length of floor = 2
breadth of floor = 1
length of side of tile = 2
No of tiles required for paving is 2.

Input: 222 332 5
Output: 3015

Approach:
It is given that edges of each tile must be parallel to edges of the tiles allows us to analyze X and Y axes separately, that is, how many segments of length ‘s’ are needed to cover a segment of length’ and ‘N’ — and take product of these two quantities.

ceil(M/s) * ceil(N/s)

, where ceil(x) is the least integer which is above or equal to x. Using integers only, it is usually written as

((M + s - 1) / s)*((N + s - 1) / s)

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the number of tiles
int solve(int M, int N, int s)
{
    // if breadth is divisible by side of square
    if (N % s == 0) {
  
        // tiles required is N/s
        N = N / s;
    }
    else {
  
        // one more tile required
        N = (N / s) + 1;
    }
  
    // if length is divisible by side of square
    if (M % s == 0) {
  
        // tiles required is M/s
        M = M / s;
    }
    else {
        // one more tile required
        M = (M / s) + 1;
    }
  
    return M * N;
}
  
// Driver Code
int main()
{
    // input length and breadth of
    // rectangle and side of square
    int N = 12, M = 13, s = 4;
  
    cout << solve(M, N, s);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation 
// of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
      
// Function to find the 
// number of tiles
static int solve(int M, int N, int s)
{
    // if breadth is divisible
    // by side of square
    if (N % s == 0
    {
  
        // tiles required is N/s
        N = N / s;
    }
    else 
    {
  
        // one more tile required
        N = (N / s) + 1;
    }
  
    // if length is divisible
    // by side of square
    if (M % s == 0
    {
  
        // tiles required is M/s
        M = M / s;
    }
    else 
    {
          
        // one more tile required
        M = (M / s) + 1;
    }
  
    return M * N;
}
  
// Driver Code
public static void main(String args[])
{
    // input length and breadth of
    // rectangle and side of square
    int N = 12, M = 13, s = 4;
  
    System.out.println(solve(M, N, s));
}
}
  
// This code is contributed 
// by ChitraNayal

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of
# above approach
  
# Function to find the number 
# of tiles 
def solve(M, N, s) :
      
    # if breadth is divisible
    # by side of square 
    if (N % s == 0) :
          
        # tiles required is N/s 
        N = N // s
          
    else :
          
        # one more tile required 
        N = (N // s) + 1
  
    # if length is divisible by 
    # side of square 
    if (M % s == 0) :
          
        # tiles required is M/s 
        M = M // s
          
    else :
          
        # one more tile required 
        M = (M // s) + 1
      
    return M *
  
# Driver Code 
if __name__ == "__main__" :
      
    # input length and breadth of 
    # rectangle and side of square
    N, M, s = 12, 13, 4
  
    print(solve(M, N, s))
              
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
  
class GFG
{
      
// Function to find the 
// number of tiles
static int solve(int M, int N, int s)
{
    // if breadth is divisible
    // by side of square
    if (N % s == 0) 
    {
  
        // tiles required is N/s
        N = N / s;
    }
    else
    {
  
        // one more tile required
        N = (N / s) + 1;
    }
  
    // if length is divisible
    // by side of square
    if (M % s == 0) 
    {
  
        // tiles required is M/s
        M = M / s;
    }
    else
    {
          
        // one more tile required
        M = (M / s) + 1;
    }
  
    return M * N;
}
  
// Driver Code
static void Main()
{
    // input length and breadth of
    // rectangle and side of square
    int N = 12, M = 13, s = 4;
  
    Console.WriteLine(solve(M, N, s));
}
}
  
// This code is contributed 
// by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function to find the number of tiles
function solve($M, $N, $s)
{
    // if breadth is divisible
    // by side of square
    if ($N % $s == 0)
    {
  
        // tiles required is N/s
        $N = $N / $s;
    }
    else 
    {
  
        // one more tile required
        $N = ($N / $s) + 1;
    }
  
    // if length is divisible
    // by side of square
    if ($M % $s == 0)
    {
  
        // tiles required is M/s
        $M = $M / $s;
    }
    else 
    {
          
        // one more tile required
        $M = ($M / $s) + 1;
    }
  
    return (int)$M * $N;
}
  
// Driver Code
  
// input length and breadth of
// rectangle and side of square
$N = 12;
$M = 13;
$s = 4;
  
echo solve($M, $N, $s);
  
// This code is contributed by mits
?>

chevron_right


Output:

12


Using ceil function:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the number of tiles
int solve(double M, double N, double s)
{
    // no of tiles
    int ans = ((int)(ceil(M / s)) * (int)(ceil(N / s)));
  
    return ans;
}
  
// Driver Code
int main()
{
    // input length and breadth of
    // rectangle and side of square
    double N = 12, M = 13, s = 4;
  
    cout << solve(M, N, s);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
class GFG
{
// Function to find the number of tiles
static int solve(double M, 
                 double N, double s)
{
    // no of tiles
    int ans = ((int)(Math.ceil(M / s)) * 
               (int)(Math.ceil(N / s)));
  
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    // input length and breadth of
    // rectangle and side of square
    double N = 12, M = 13, s = 4;
  
    System.out.println(solve(M, N, s));
}
}
  
// This Code is contributed by mits

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of 
# above approach
import math
  
# Function to find the
# number of tiles
def solve(M, N, s):
  
    # no of tiles
    ans = ((math.ceil(M / s)) * 
           (math.ceil(N / s)));
  
    return ans
  
# Driver Code
if __name__ == "__main__":
      
    # input length and breadth of
    # rectangle and side of square
    N = 12
    M = 13
    s = 4
  
    print(solve(M, N, s))
  
# This code is contributed 
# by ChitraNayal 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
class GFG
{
// Function to find the number of tiles
static int solve(double M, 
                double N, double s)
{
    // no of tiles
    int ans = ((int)(Math.Ceiling(M / s)) * 
            (int)(Math.Ceiling(N / s)));
  
    return ans;
}
  
// Driver Code
public static void Main()
{
    // input length and breadth of
    // rectangle and side of square
    double N = 12, M = 13, s = 4;
  
    Console.WriteLine(solve(M, N, s));
}
}
  
// This Code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of 
// above approach
  
// Function to find the
// number of tiles
function solve($M, $N, $s)
{
    // no of tiles
    $ans = ((int)(ceil($M / $s)) * 
            (int)(ceil($N / $s)));
  
    return $ans;
}
  
// Driver Code
  
// input length and breadth of
// rectangle and side of square
$N = 12;
$M = 13;
$s = 4;
  
echo solve($M, $N, $s);
  
// This code is contributed by mits
?>

chevron_right


Output:

12


My Personal Notes arrow_drop_up

Do your best to show the world what you are capable of

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.