## Introduction to ceil function in C++

ceil is a function that provides the next possible greater than or an equal integer number as output to a random number provided as the input in the form of a parameter. This function is generally defined under the library: <cmath>. This function represents the upper limit. In many scenarios, we need to obtain the upper and lower limits of a function or value which can be easily done by applying this function.

### Syntax

This ceil function is a default function available in the standard library. The syntax of ceil is similar to be like a simple function. There would be a parameter value that is passed inside the ceil function to get the lowest possible value which is greater than or equal to the parametric value.

`Data_type ceil(data_type variable_number);`

It takes a single parameter value as its argument and returns an integer value. In general, there are many functions that are present which makes many problem statements easier to solve. One of the functions is “Ceil”.

And, in general, the ceil function is used in parallel with the floor function. These functions precisely give the integer value with respect to the lower and higher value for the floating-point number given in the parameter.

### Examples of ceil function in C++

Let us see different examples in getting to know the ”ceil” functions:

#### Example #1

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x;
int y;
cout<<"Enter any float number: ";
cin>>x;
y=ceil(x);
cout<<"The ceil function value of folating point number x is: "<<y;
}
```

**Output:**

#### Example #2

Now, let us see an example for the integer value in the parameter.

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
int y;
cout<<"Enter any integer number: ";
cin>>x;
y=ceil(x);
cout<<"The ceil function value of integer point number x is: "<<y;
}
```

**Output:**

#### Example #3

Below, let us have an example for both ceil and floor functions.

4 Online Courses | 5 Hands-on Projects| 37+ Hours| Verifiable Certificate of Completion

4.5

View Course

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x;
float y,z;
cout<<"Enter any integer number: ";
cin>>x;
y=ceil(x);
z=floor(x);
cout<<"The ceil function value of integer point number x is: "<<y<<endl;
cout<<"The floor function value of integer point number x is: "<<z;
}
```

**Output:**

Here we can clearly observe that the value 20.5 is made to 21 by using the ceil function and is reduced to 20 if the floor function is used.

#### Example #4

We use the data type as integer instead of float.

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
float y,z;
cout<<"Enter any integer number: "
cin>>x;
y=ceil(x);
z=floor(x);
cout<<"The ceil function value of integer point number x is: "<<y<<endl;
cout<<"The floor function value of integer point number x is: "<<z;
}
```

**Output:**

The only difference for the programs written above and below is with respect to the highlighted data type.

Since, the input value is the integer data type in the below program, though we give the input in a decimal format the compiler stores it as an integer value only. That is the reason we get the same ceil and floor value for the data given as input for the data type integer.

#### Example #5

Now, to provide the ceil value for the integer numbers, instead of using the data type “int”, we can use “double”.

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x;
float y,z;
cout<<"Enter any integer number: ";
cin>>x;
y=ceil(x);
cout<<"The ceil function value of integer point number x is: "<<y<<endl;
}
```

**Output:**

#### Example #6

Here, let us check an example of the negative input values.

**Code:**

```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x;
float y,z;
cout<<"Enter any integer number: ";
cin>>x;
y=ceil(x);
cout<<"The ceil function value of integer point number x is: "<<y<<endl;
}
```

**Output:**

As an exercise try providing different data types and any other scenarios in getting to know about the ceil function*.*

### Conclusion

In an above-discussed way, we have the ceil function defined and we have successfully implemented the same through C++ programming language. We have also checked the main difference between the floor and ceil functions and how the compiler returns the output based on the data types used and in negative scenarios too.

### Recommended Articles

This is a guide to ceil function in C++. Here we discuss the introduction and various examples of ceil function in C++ along with code implementation. You may also have a look at the following articles to learn more –