Find ceil of a/b without using ceil() function

Given a and b, find the ceiling value of a/b without using ceiling function.

Examples:

Input : a = 5, b = 4 
Output : 2 
Explanation: a/b = ceil(5/4) = 2 

Input : a = 10, b = 2
Output : 5 
Explanation: a/b = ceil(10/2) = 5 

The problem can be solved using ceiling function, but the ceiling function does not work when integers are passed as parameters. Hence there are following 2 approaches below to find the ceiling value.



Approach 1:

ceilVal = (a / b) + ((a % b) != 0)

a/b returns the integer division value, and ((a % b) != 0) is a checking condition which returns 1 if we have any remainder left after the division of a/b, else it returns 0. The integer division value is added with the checking value to get the ceiling value.

Given below is the illustration of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find ceil(a/b)
// without using ceil() function
#include <cmath>
#include <iostream>
using namespace std;
  
// Driver function
int main()
{
    // taking input 1
    int a = 4;
    int b = 3;
    int val = (a / b) + ((a % b) != 0);
    cout << "The ceiling value of 4/3 is " 
         << val << endl;
  
    // example of perfect division
    // taking input 2
    a = 6;
    b = 3;
    val = (a / b) + ((a % b) != 0);
    cout << "The ceiling value of 6/3 is " 
         << val << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find 
// ceil(a/b) without 
// using ceil() function
import java.io.*;
  
class GFG
{
    // Driver Code
    public static void main(String args[])
    {
        // taking input 1
        int a = 4;
        int b = 3, val = 0;
        if((a % b) != 0)
            val = (a / b) + 
                  (a % b);
        else
            val = (a / b);
        System.out.println("The ceiling "
                       "value of 4/3 is "
                                      val);
        // example of perfect
        // division taking input 2
        a = 6;
        b = 3;
        if((a % b) != 0)
            val = (a / b) + (a % b);
        else
            val = (a / b);
        System.out.println("The ceiling "
                       "value of 6/3 is "
                                      val);
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find ceil(a/b)
# without using ceil() function
import math
  
# Driver Code
  
# taking input 1
a = 4;
b = 3;
val = (a / b) + ((a % b) != 0);
print("The ceiling value of 4/3 is",
                   math.floor(val));
  
# example of perfect division
# taking input 2
a = 6;
b = 3;
val = int((a / b) + ((a % b) != 0));
print("The ceiling value of 6/3 is", val);
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find ceil(a/b)
// without using ceil() function
using System;
  
class GFG
{
  
    // Driver Code
    static void Main()
    {
        // taking input 1
        int a = 4;
        int b = 3, val = 0;
        if((a % b) != 0)
            val = (a / b) + (a % b);
        else
            val = (a / b);
        Console.WriteLine("The ceiling "
                      "value of 4/3 is "
                                     val);
      
        // example of perfect
        // division taking input 2
        a = 6;
        b = 3;
        if((a % b) != 0)
            val = (a / b) + (a % b);
        else
            val = (a / b);
        Console.WriteLine("The ceiling "
                      "value of 6/3 is "
                                     val);
    }
}
// This code is contributed by 
// Manish Shaw(manishshaw1)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find ceil(a/b)
// without using ceil() function
  
// Driver function
  
    // taking input 1
    $a = 4;
    $b = 3;
    $val = ($a / $b) + (($a % $b) != 0);
    echo "The ceiling value of 4/3 is "
        , floor($val) ,"\n";
  
    // example of perfect division
    // taking input 2
    $a = 6;
    $b = 3;
    $val = ($a / $b) + (($a % $b) != 0);
    echo "The ceiling value of 6/3 is "
                               , $val ;
  
// This code is contributed by anuj_67.
  
?>

chevron_right


Output:

The ceiling value of 4/3 is 2
The ceiling value of 6/3 is 2

Approach 2:

ceilVal = (a+b-1) / b

Using simple maths, we can add the denominator to the numerator and subtract 1 from it and then divide it by denominator to get the ceiling value.

Given below is the illustration of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find ceil(a/b)
// without using ceil() function
#include <cmath>
#include <iostream>
using namespace std;
  
// Driver function
int main()
{
    // taking input 1
    int a = 4;
    int b = 3;
    int val = (a + b - 1) / b;
    cout << "The ceiling value of 4/3 is " 
         << val << endl;
  
    // example of perfect division
    // taking input 2
    a = 6;
    b = 3;
    val = (a + b - 1) / b;
    cout << "The ceiling value of 6/3 is " 
         << val << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find ceil(a/b)
// without using ceil() function
  
class GFG {
      
// Driver Code
public static void main(String args[])
{
      
    // taking input 1
    int a = 4;
    int b = 3;
    int val = (a + b - 1) / b;
    System.out.println("The ceiling value of 4/3 is "
                        + val);
  
    // example of perfect division
    // taking input 2
    a = 6;
    b = 3;
    val = (a + b - 1) / b;
    System.out.println("The ceiling value of 6/3 is "
                        + val );
}
}
  
// This code is contributed by Jaideep Pyne

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# math.ceil(a/b) without 
# using math.ceil() function
import math
  
# Driver Code
# taking input 1
a = 4;
b = 3;
val = (a + b - 1) / b;
print("The ceiling value of 4/3 is "
                    math.floor(val));
  
# example of perfect division
# taking input 2
a = 6;
b = 3;
val = (a + b - 1) / b;
print("The ceiling value of 6/3 is "
                    math.floor(val));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find ceil(a/b)
// without using ceil() function
using System;
  
class GFG {
      
    // Driver Code
    public static void Main()
    {
          
        // taking input 1
        int a = 4;
        int b = 3;
        int val = (a + b - 1) / b;
        Console.WriteLine("The ceiling"
          + " value of 4/3 is " + val);
      
        // example of perfect division
        // taking input 2
        a = 6;
        b = 3;
        val = (a + b - 1) / b;
        Console.WriteLine("The ceiling"
         + " value of 6/3 is " + val );
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find ceil(a/b)
// without using ceil() function
  
    // Driver function
    // taking input 1
    $a = 4;
    $b = 3;
    $val = ($a + $b - 1) /$b;
    echo "The ceiling value of 4/3 is "
        , floor($val) ,"\n";
  
    // example of perfect division
    // taking input 2
    $a = 6;
    $b = 3;
    $val = ($a + $b - 1) / $b;
    echo "The ceiling value of 6/3 is "
        ,floor($val) ;
  
// This code is contributed by anuj_67.
?>

chevron_right


Output:

The ceiling value of 4/3 is 2
The ceiling value of 6/3 is 2


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.