How to use the ceiling function in C++?

You can use the ceil() function in C++ to achieve rounding up. The ceil() function is defined in the header file.

Here is an example of using the ceil() function:

#include <iostream>
#include <cmath>

int main() {
    double num1 = 3.14;
    double num2 = 5.6;

    double ceilNum1 = ceil(num1);
    double ceilNum2 = ceil(num2);

    std::cout << "Ceiling of " << num1 << " is " << ceilNum1 << std::endl;
    std::cout << "Ceiling of " << num2 << " is " << ceilNum2 << std::endl;

    return 0;
}

The output is:

Ceiling of 3.14 is 4
Ceiling of 5.6 is 6

In the example, the ceil() function is used to round up the values of num1 and num2. The ceil() function takes a floating point number as a parameter and returns the smallest integer greater than or equal to that floating point number.

bannerAds