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 the ceiling function, but the ceiling function does not work when integers are passed as parameters. Hence there are the 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++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a = 4;
int b = 3;
int val = (a / b) + ((a % b) != 0);
cout << "The ceiling value of 4/3 is "
<< val << endl;
a = 6;
b = 3;
val = (a / b) + ((a % b) != 0);
cout << "The ceiling value of 6/3 is "
<< val << endl;
return 0;
}
|
Java
class GFG
{
public static void main(String args[])
{
System.out.println( "The ceiling " +
"value of 4/3 is " +
intCeil( 4 , 3 ));
System.out.println( "The ceiling " +
"value of 5/3 is " +
intCeil( 5 , 3 ));
System.out.println( "The ceiling " +
"value of 6/3 is " +
intCeil( 6 , 3 ));
}
public static int intCeil( int a, int b) {
if (a % b != 0 ) {
return (a / b) + 1 ;
} else {
return (a / b);
}
}
}
|
Python3
import math
a = 4 ;
b = 3 ;
val = (a / b) + ((a % b) ! = 0 );
print ( "The ceiling value of 4/3 is" ,
math.floor(val));
a = 6 ;
b = 3 ;
val = int ((a / b) + ((a % b) ! = 0 ));
print ( "The ceiling value of 6/3 is" , val);
|
C#
using System;
class GFG
{
static void Main()
{
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);
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);
}
}
|
PHP
<?php
$a = 4;
$b = 3;
$val = ( $a / $b ) + (( $a % $b ) != 0);
echo "The ceiling value of 4/3 is "
, floor ( $val ) , "\n" ;
$a = 6;
$b = 3;
$val = ( $a / $b ) + (( $a % $b ) != 0);
echo "The ceiling value of 6/3 is "
, $val ;
?>
|
Javascript
<script>
var a = 4;
var b = 3, val = 0;
if ((a % b) != 0)
val = parseInt(a / b) + (a % b);
else
val = parseInt(a / b);
document.write( "The ceiling " + "value of 4/3 is " + val+ "<br/>" );
a = 6;
b = 3;
if ((a % b) != 0)
val = parseInt(a / b) + (a % b);
else
val = parseInt(a / b);
document.write( "The ceiling " + "value of 6/3 is " + val);
</script>
|
Output: The ceiling value of 4/3 is 2
The ceiling value of 6/3 is 2
Time Complexity: O(1)
Auxiliary Space: O(1)
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++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a = 4;
int b = 3;
int val = (a + b - 1) / b;
cout << "The ceiling value of 4/3 is "
<< val << endl;
a = 6;
b = 3;
val = (a + b - 1) / b;
cout << "The ceiling value of 6/3 is "
<< val << endl;
return 0;
}
|
Java
class GFG {
public static void main(String args[])
{
int a = 4 ;
int b = 3 ;
int val = (a + b - 1 ) / b;
System.out.println( "The ceiling value of 4/3 is "
+ val);
a = 6 ;
b = 3 ;
val = (a + b - 1 ) / b;
System.out.println( "The ceiling value of 6/3 is "
+ val );
}
}
|
Python3
import math
a = 4 ;
b = 3 ;
val = (a + b - 1 ) / b;
print ( "The ceiling value of 4/3 is " ,
math.floor(val));
a = 6 ;
b = 3 ;
val = (a + b - 1 ) / b;
print ( "The ceiling value of 6/3 is " ,
math.floor(val));
|
C#
using System;
class GFG {
public static void Main()
{
int a = 4;
int b = 3;
int val = (a + b - 1) / b;
Console.WriteLine( "The ceiling"
+ " value of 4/3 is " + val);
a = 6;
b = 3;
val = (a + b - 1) / b;
Console.WriteLine( "The ceiling"
+ " value of 6/3 is " + val );
}
}
|
PHP
<?php
$a = 4;
$b = 3;
$val = ( $a + $b - 1) / $b ;
echo "The ceiling value of 4/3 is "
, floor ( $val ) , "\n" ;
$a = 6;
$b = 3;
$val = ( $a + $b - 1) / $b ;
echo "The ceiling value of 6/3 is "
, floor ( $val ) ;
?>
|
Javascript
<script>
var a = 4;
var b = 3;
var val = (a + b - 1) / b;
document.write( "The ceiling value of 4/3 is " + val+ "<br/>" );
a = 6;
b = 3;
val = parseInt((a + b - 1) / b);
document.write( "The ceiling value of 6/3 is " + val);
</script>
|
Output: The ceiling value of 4/3 is 2
The ceiling value of 6/3 is 2
Time Complexity: O(1)
Auxiliary Space: O(1)