Updated July 4, 2023
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 a function’s upper and lower limits or values, 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. A parameter value would be passed inside the ceil function to get the lowest possible value, 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, many functions are present, making 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 concerning the lower and higher value for the floating-point number provided 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 of 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 of both ceil and floor functions.
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 observe that the value 20.5 is made to 21 using the ceil function and is reduced to 20 if the floor function is used.
Example #4
We use the data type as an integer instead of a 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 between the programs written above and below concerns 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 why we get the same ceil and floor value for the data given as input for the data type integer.
Example #5
Now, instead of using the data type “int” to provide the ceil value for the integer numbers, 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 defined the ceil function and 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 –