Home C++ Programming Tutorials Ceil and Floor functions in C++

Ceil and Floor functions in C++

by anupmaurya
2 minutes read

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 : 3.5
Output : 3

Input : -3.1
Output : -4

Input : 1.9
Output : 1
// 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(3.3) << endl; 
	cout << "Floor is : " << floor(-3.3) << endl; 

	return 0; 
} 

Output:

Floor is : 3
Floor is : -4

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 : 29.5
Output : 30

Input : -23.1
Output : -24

Input : 21.9
Output : 22
// C++ program to demonstrate ceil function 
#include <iostream> 
#include <cmath> 
using namespace std; 


int main() 
{ 
	// using ceil function which return 
	// floor of input value 
	cout << " Ceil is : " << ceil(3.3) << endl; 
	cout << " Ceil is : " << ceil(-6.3) << endl; 

	return 0; 
} 

Output:

Ceil is : 4
Ceil is : -7

This article is contributed by Ravi Gupta. If you like Techarge and would like to contribute, you can also write an article using https://techarge.in/start-blogging-with-us/ or mail your article to techarge.in@gmail.com . See your article appearing on the Techarge home page and help other to learn. 🙂

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.