Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

floor
ceiling
function
method
c++

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

Educative Answers Team

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

RELATED TAGS

floor
ceiling
function
method
c++
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring