In this C++ tutorial, you will learn how to find the floor value of given decimal number using floor() function of cmath, with syntax and examples.

C++ floor()

C++ floor() returns floor value of given decimal number.

Floor value is the largest integer value less than the given number.

Syntax

The syntax of C++ floor() is

floor(x)

where

ParameterDescription
xA double, float, long double, or any integral type value.

Returns

The return value depends on the type of value passed for parameter x.

The return value of floor(x) is

  • double if x is double.
  • float if x is float.
  • long double if x is long double.

The synopsis of floor() function is

double floor(double x);
float floor(float x);
long double floor(long double x);
double floor(T x); // For integral type

floor() is a function of cmath library. Include cmath library in the program, if using floor() function.

ADVERTISEMENT

Example

In this example, we read a double value from user into x, and find its floor value using floor() function.

C++ Program

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x;
    cout << "Enter a number : ";
    cin >> x;
    
    double result = floor(x);
    cout << "floor(" << x << ") : " << result << endl;
}

Output

Enter a number : 3.14
floor(3.14) : 3
Program ended with exit code: 0
Enter a number : 3.89
floor(3.89) : 3
Program ended with exit code: 0
Enter a number : 3
floor(3) : 3
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ floor(), and how to use this function to find the floor value of given number, with the help of examples.