What are the floor() and ceil() functions in C++?

The floor() and ceil() methods are found in the cmath header file.

svg viewer

floor()

The floor() method returns the largest possible integer less than or equal to the number passed as an argument.

Code

#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Floor of -1.5 is " << floor(-1.5) << endl;
cout << "Floor of 5.9 is " << floor(5.9) << endl;
return 0;
}

ceil()

The ceil() function returns the smallest possible integer greater than or equal to the number passed as an argument.

Code

#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Ceiling of -3.3 is " << ceil(-3.3) << endl;
cout << "Ceiling of 8.1 is " << ceil(8.1) << endl;
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved