Math Floor Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Floor Function

Math Floor Function

BLUF: Mastering Math Floor Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Floor Function

C++ is renowned for its efficiency. Learn how Math Floor Function enables low-level control and high-performance computing in the tutorial below.

C++ Math floor

It approximates the number to the closest whole number that is less than or equal to the provided value.

For example:

Example

floor(8.2)=8.0;
floor(-8.8)=-9.0;

Syntax

Suppose a number is 'x'. Syntax would be :

Example

double floor(double x);

Parameter

x : It represents a value that is rounded to the closest whole number.

Return value

It returns the value of x rounded down to the nearest integer.

Example 1

Let's examine a basic illustration using a non-negative number.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
 float x=7.8;
 std::cout << "Initial value of x is : " << x<<std::endl;
cout<<"Now, the value of x is :"<<floor(x);
 return 0;
}

Output:

Output

Initial value of x is : 7.8
Now, the value of x is :7

Example 2

Let's explore a basic illustration using a negative value.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
 float x=-10.2;
 std::cout << "Initial value of x is : " << x<<std::endl;
cout<<"Now, the value of x is :"<<floor(x);
 return 0;
}

Output:

Output

Initial value of x is : -10.2
Now, the value of x is :-11

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience