C++

# Floor C++

In C++, the floor() function returns the value which is smaller than or equal to the specified value. For the programs in which we use the floor() function, we must include the <cmath>header file. This header file is declared at the start of every program. The specified decimal floor value is returned by this function. That return number always depends on the kind of value passed as an argument of the floor() function. We need a single value to calculate the floor value. The floor() function contains different parameters. For example, double, float, and long double. The data types of the declared variable are as given. We’ll go over the floor() method in detail with illustrations in this article.

We utilize software DEV C++ to do the coding in C++. For running the code, first, we have to compile the code and then run that program. We compile and run the code separately or together. It all depends on us.

## Example 1: Use floor() function to floating-point number

In C++ the floor() function is applied to floating-point values. In this instance, we include headers files <iostream>and <cmath>. When we write the programs in which we apply the floor() function, we must include header file <cmath>. We begin with the main() function. In C++, floating-point variables are utilized to store the floating-point values. We use double for the declaration of the floating-point number. The data type float is omitted when it is led by double-type contestants in the declaration statement.

#include <iostream>

#include<cmath>

using namespace std;

int main() {

double a;

cout <<"Enter a float number : ";

cin >>a;

double result = floor(a);

cout <<"floor(" <<a <<") : " <<result <<endl;

}

The user enters a random floating-point number. We apply the cout() function here. The variable ‘a’ is used to store that floating-point number. “Cin” is applied to store the number. We use another variable double result. The floating-point number that the user entered is passed as a parameter of the floor() function. In the end, the cout function is applied. And it shows the floor value of the entered float number.

The user entered the ‘8.23’ floating-point number. We apply the floor() function to this floating-point number. It rounds the number to the adjacent integer that is not more than the particular value. So after applying the floor() function to 8.23, we get the output 8.

## Example 2: Use floor() function to negative number

We are going to utilize the floor() for a negative number and see what happens. In this program after including the headers files, we write a program to validate the floor() function. We use the namespace std here. After that, we apply the main() function which is a driver function. We consider the negative number that is -5.70. We apply the floor() function to this negative number. The text we want to be printed is ‘floor of value -5.70’. At the end of the program, we use endl. It is utilized to add a new character.

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

cout <<"Floor of value -5.70 : " <<floor(-5.70) <<endl;

return 0;

}

In the output after applying the floor() function to -5.70, we get -6. By using the floor() function we get the largest possible integer which is less than or equal to -5.70.

Example 3: Use floor() function to an integer number:

This code starts with two header files that are <iostream>and <cmath>. We apply the driver function which is basically our main function. We use the variable ‘res’ to store the result after applying the floor() function to the integer. Then we take integer numbers. That integer number is passed as a parameter to the floor() function. All the variables in the program must be declared. In this program, ‘int’ shows the data type and it is followed by the name of the variable ‘number’. The cout function is applied to print the text ‘floor of number 46’.

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double res;

int number = 46;

res = floor(number);

cout <<"Floor of number " <<number <<" = " <<res;

return 0;

}

The floor of an integer number is the same as the integer number itself. Here we take the “46” as integer value and after applying the floor() function to this integer, we get the same integer number 46.

## Example 4:

Here, we use header files at the start of the code i.e. #include <iostream>and #include <math.h>. We utilize the main function. We take a floating-point value of ‘9.4578’. Then, we want to put the floor() function on this floating-point number. The variable used to store that floating-point value is ‘f’. The cout statement is used here to take the print of two statements. And after applying the floor() function, cout prints the statement i.e. ‘Now the value of that number is’.

#include <iostream>

#include<math.h>

using namespace std;

int main()

{

float f=9.4578;

std::cout <<"The real value of float number is : " <<f<<std::endl;

cout<<"Now the value of that number is :"<<floor(f);

return 0;

}

In the output, we get the floor value 9 of the floating-point value ‘9.4578’.

## Conclusion:

In this article, we talked about the floor() function in C++. We have observed different examples of floor() functions. We take a floating-point value and see what happens after applying the floor() function to that floating-point value. In the next examples, we have taken a negative value and any integer. We have also applied the floor() function to that negative value and integer. We have noticed the output of all examples as well.