Examples of ceil function in C++

Written by Sagar RabidasFebruary 12, 2022
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Sagar Rabidas

Software Developer

ceil function in C++

Ceil is a characteristic that provides the next feasible extra than or an equal integer number as output to a random range supplied because the enter inside the shape of a parameter. This feature is commonly described under the library: <cmath>. This characteristic represents the higher restriction. In many situations, we need to achieve the upper and lower limits of a feature or fee which can be without problems performed with the aid of making use of this function.

Syntax:-

This ceil function is a default function available in the standard library. The syntax of ceil is similar to being 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, many functions 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 concerning 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;`
`}`

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;`
`}`

Example #3

Below, let us have an example for 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;`
`}`

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;`
`}`

Example #5

Now, to provide the ceiling 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;`
`}`

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;`
`}`

C
C++
ceil function
ceil function in C++
C++ function
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
You must be Logged in to comment
Code Block
Sagar Rabidas
Software Developer
+8 more
300 Blog Posts
Trending Technologies
15
Software40
DevOps47
Frontend Development24
Backend Development20
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.