Skip to content
Related Articles

Related Articles

Ceil and Floor functions in C++

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 23 Jun, 2022

In mathematics and computer science, the floor and ceiling functions map a real number to the greatest preceding or the least succeeding integer, respectively. floor(x) : Returns the largest integer that is smaller than or equal to x (i.e : rounds downs the nearest integer).

// Here x is the floating point value.
// Returns the largest integer smaller 
// than or equal to x 
double floor(double x)  

Examples of Floor:

Input : 2.5
Output : 2

Input : -2.1
Output : -3

Input : 2.9
Output : 2

CPP




// C++ program to demonstrate floor function
#include <iostream>
#include <cmath>
using namespace std;
 
// Driver function
int main()
{
    // using floor function which return
    // floor of input value
    cout << "Floor is : " << floor(2.3) << endl;
    cout << "Floor is : " << floor(-2.3) << endl;
 
    return 0;
}

Output:

Floor is : 2
Floor is : -3

  ceil(x) : Returns the smallest integer that is greater than or equal to x (i.e : rounds up the nearest integer).

// Here x is the floating point value.
// Returns the smallest integer greater 
// than or equal to x 
double ceiling(double x)  

Examples of Ceil:

Input : 2.5
Output : 3

Input : -2.1
Output : -2

Input : 2.9
Output : 3

CPP




// C++ program to demonstrate ceil function
#include <iostream>
#include <cmath>
using namespace std;
 
// Driver function
int main()
{
    // using ceil function which return
    // floor of input value
    cout << " Ceil is : " << ceil(2.3) << endl;
    cout << " Ceil is : " << ceil(-2.3) << endl;
 
    return 0;
}

Ceil is : 3
Ceil is : -2

Let us see the differences in a tabular form -:

 ceilfloor
1.It is used to return the smallest integral value n that is not less than n.It is used to return the largest integral value n that is not greater than n.
2.It rounds the n upwards.It rounds the n downwards.
3.

Its syntax is -:

data_type ceil (n);

Its syntax is -:

data_type floor (n);

4.It takes only one parameter that is the value to round up.It takes only one parameter that is the value to round up.

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!