Function ceil() in C++

ceil Function of cmath Header File in C++

On this page we will discuss about function ceil() which is used in C++. The cmath header file is a C++ standard library header file that contains a set of math functions and macro definitions that are used to perform mathematical operations. The ceil function returns the smallest integer that is greater than or equal to a given number.
ceil() in c++

ceil() Function in C++

In C++ programming language the ceil function is included in cmath header file.

It is used to round a number up to the next integer and the range of the ceil function is the set of all real numbers. The datatype of parameter can be double/ float/ long double only.

Declaration of ceil function

double ceil(double x) 

Parameters of ceil function

The ceil function takes a single argument of type double.

ParameterDescription
xIt is the number that you want to round up to the next integer.

Return value of ceil function

The ceil function returns a value of type double, which is the smallest integer that is greater than or equal to the input number.

Implementation of  ceil function in C++

Example 1:

The following code shows the use of ceil function.

Run
#include <iostream> 
#include <cmath>
using namespace std;

int main() {

  // Initializing parameter
  double x = 4.6;
  double y = ceil(x);
  cout << "The smallest integer greater than or equal to " << x << " is " << y << endl;

  return 0;
}

Output:

The smallest integer greater than or equal to 4.6 is 5

Example 2:

Run
#include <iostream>
#include <cmath>
using namespace std;

int main() {
  
  // Negative floating point number as parameter  
  double x = -3.2;
  double y = ceil(x);
  cout << "The smallest integer greater than or equal to " << x << " is " << y << endl;

  // Integer as parameter
  int z = 4;
  int w = ceil(z);
  cout << "The smallest integer greater than or equal to " << z << " is " << w << endl;
  return 0;
}

Output:

The smallest integer greater than or equal to -3.2 is -3
The smallest integer greater than or equal to 4 is 4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription