Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
What is floor() in C++?
1.1.
floor() syntax in C++
1.2.
Return value
1.3.
Program using floor() function
2.
What is ceil() in C++?
2.1.
ceil() syntax in C++
2.2.
Return value
3.
Frequently Asked Questions
3.1.
Which header file floor() and ceil() functions are present?
3.2.
What is floor and ceil in C++?
3.3.
What is the floor() function in C++?
3.4.
Explain the syntax of the floor() and ceil() functions in C++.
3.5.
What is the Ceil() function in C++?
4.
Conclusions
Last Updated: Aug 27, 2024
Easy

Floor and Ceil Functions in C++

Author Gunjan Batra
0 upvote

In programming dealing with integer values is more manageable and straightforward than decimal values. Working with integer values is easy and helps get accurate results. In c++, we have the floor and ceil functions to solve this problem. Floor and Ceil in c++ are those functions that help provide the integer value nearest to that decimal value. 

Floor and Ceil functions in C++

In this blog, we will learn about floor and Ceil in C++. Also see, Literals in C.

What is floor() in C++?

The floor is a function in c++ that is defined and described in the cmath header file.

floor() in C++
  • This function returns the largest possible integer that is equal to or smaller than the input number.
     
  • It gives the downward value of the number.
     
  • The floor() function takes a single input parameter and returns a single value integer. 

floor() syntax in C++

The syntax is similar to a normal function. 

floor(number);

The parameters of floor() function takes are shown below:

  1. int floor(int number);
     
  2. float floor(float number);
     
  3. double floor(double number);
     
  4. long double floor( long double number);
     

Return value

floor() returns the largest possible integer equal to or smaller than the input number. 

Examples of floor()

  • Input: 10.25     Output: 10
     
  • Input: 111.89   Output: 111
     
  • Input: -2.6       Output: -3
     

Program using floor() function

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float num= 11.66;
float var = -3.6;
cout<<"floor value of the num is "<<floor(num)<<endl;
cout<<"floor value of the var is "<<floor(var)<<endl;
return 0;
}


Output

Output of floor Function Program

What is ceil() in C++?

 Ceil is a function in c++ that is defined and described in the cmath header file.

ceil() in C++
  • This function returns the smallest possible integer that is greater than or equal to the input number.
     
  • It gives the upward value of the number.
     
  • The ceil() function takes a single input parameter and returns a single value integer. 

ceil() syntax in C++

ceil(number);

The parameters this function takes are shown below:

  1. int ceil(int number);
     
  2. float ceil(float number);
     
  3. double ceil(double number);
     
  4. long double ceil( long double number);
     

Return value

ceil() returns the smallest possible integer that is equal to or smaller than the input number. 

Examples of ceil()

  • Input: 10.5     Output: 11
     
  • Input: 111.8   Output: 112
     
  • Input: 3.7       Output: 4
     

Program using ceil() function

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float num= 11.66;
float var = -3.6;
cout<<" Ceil value of the num is "<<ceil(num)<<endl;
cout<<" Ceil value of the var is "<<ceil(var)<<endl;
return 0;
}


Output

Output of Ceil Function Program

You practice by yourself with the help of online c++ compiler.

Must Read Swap Two Numbers Without Using Third Variable

Frequently Asked Questions

Which header file floor() and ceil() functions are present?

The definition of floor() and ceil() is present in the C++ library. It is defined and described in the cmath header file.

What is floor and ceil in C++?

The floor and ceil in C++ are the functions that programmers use to get the rounded value of the decimal numbers. 

What is the floor() function in C++?

The floor() function in C++ returns the largest possible integer equal to or smaller than the input number. It gives the downward value of the input number. This function shows the lower limit of the integer value. 

Explain the syntax of the floor() and ceil() functions in C++.

The syntax of the floor()  and ceil() function is given below:

floor(number) and ceil(number). The number can be of any data type. It can be double, float, or long double. 

What is the Ceil() function in C++?

The ceil() function in C++ returns the smallest possible integer equal to or larger than the input number. It gives the upward value of the input number. This function shows the upper limit of the integer value. 

Conclusions

In this blog, we discussed floor and ceil in C++. We briefly learn about the definition, syntax, and return type. We also look at the working of these functions using codes.

Refer to our guided paths on Code360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.

Happy Coding!

Live masterclass