Ceil and floor functions in C++


The ceil Function

The ceil function returns the smallest possible integer value which is equal to the value or greater than that. This function is declared in “cmath” header file in C++ language. It takes single value whoes ceil value is to be calculated. The datatype of variable should be double/float/long double only.

Here is the syntax of ceil function in C++ language,

double ceil(double x);
float ceil(float x);

Here is an example of ceil function in C++ language,

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   float var = 1234.25;
   float res;
   res = ceil(var);
   cout << "Ceil value of " << var << " = " << res << endl;
   return 0;
}

Output

Ceil value of 1234.25 = 1235

The floor Function

The floor function returns the largest possible integer value which is equal to the value or smaller than that. This function is also declared in “cmath” header file in C++ language. It takes single value whoes floor value is to be calculated. The datatype of variable should be double/ float/ long double only.

Here is the syntax of floor function in C++ language,

double floor(double x);
float floor(float x);

Here is an example of floor in C++ language,

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   float var = 211.876;
   float res;
   res = floor(var);
   cout << "Floor value of " << var << " = " << res << endl;
   return 0;
}

Output

Floor value of 211.876 = 211

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 24-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements