Find ceil of a/b without using ceil() function in C++.


Here we will see how to get the ceiling value of a/b without using the ceil() function. If a = 5, b = 4, then (a/b) = 5/4. ceiling(5/4) = 2. To solve this, we can follow this simple formula −

$$ceil\lgroup a,b\rgroup=\frac{a+b-1}{b}$$

Example

 Live Demo

#include<iostream>
using namespace std;
int ceiling(int a, int b) {
   return (a+b-1)/b;
}
int main() {
   cout << "Ceiling of (5/4): " << ceiling(5, 4) <<endl;
   cout << "Ceiling of (100/3): " << ceiling(100, 3) <<endl;
   cout << "Ceiling of (49/7): " << ceiling(49, 7) <<endl;
}

Output

Ceiling of (5/4): 2
Ceiling of (100/3): 34
Ceiling of (49/7): 7

Updated on: 29-Oct-2019

659 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements