
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Ceil of a Number 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
#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
Advertisements