# Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than or equal to the given float or double value. For example, if we give 3.66 as input, it returns 3.

**Syntax**

floor(float value or double value);

**Parameters**

This function takes a single float value or double value as a parameter. A number which has a decimal part or fractional part is called a float or double number. Ex: 3.66, 5.987 etc., we can directly pass a number into the floor() function, or we can give a variable which is storing the number.

**Return value**

It returns an integer value less than or equal to the given input value.

The difference between the round function and floor() function is the round function returns an integer near the given input, whereas the floor function returns an integer value less than or equal to the given input.

We can use floor function in the following three ways.

```
// program to show the implementation of floor() function in different ways
#include <stdio.h>
#include <math.h>
int main () {
// Declaring the variables locally
float num1, num2, num3, num4;
float answer1, answer2;
// Assigning values to the declared variables
num1 = 56.987;
num2 = 78.456;
num3 = 64.143l;
num4 = 15.786;
printf(" \nPrinting the floor values: ");
// First way to use floor() function
// Directly returning the value in the printf statement
// calling the floor() function in printf statement.
printf(" \nfloor value of num1 is = %.1lf", floor(num1));
printf(" \nfloor value of num2 is = %.1lf", floor(num2));
printf(" \nfloor value of num3 is = %.1lf", floor(num3));
// Second way to use floor() function
// Returning the floor value to a variable
// pass a variable into the floor function.
answer1 = floor(num4);
// printing the variable
printf(" \nfloor value of num4 is = %.1f", answer1);
// Third way to use floor() function
// Directly passing the numerical value in the floor() function
answer2 = floor(9.99);
//printing the floor value of the numerical value
printf(" \nfloor value of value is = %.1f", answer1);
return(0);
}
```

**Output:**

### Example Programs on Floor Function

**Example 1: Taking float value as input**

```
// program to implement floor() function in C
#include <stdio.h>
#include <math.h>
int main() {
float n; // declaring a float variable
printf(" \nEnter a float value: ");
// reading the float value
scanf("%f", &n);
// finding the floor value of the given float value
int r = floor(n); // storing the integer value in the r variable
// printing the floor value of the given input
printf(" \nThe floor value of %.2f is = %d", n, r);
return 0;
}
```

**Output:**

**Example 2: Taking double value as input**

```
// program to implement floor() function in C
#include <stdio.h>
#include <math.h>
int main() {
double n; // declaring a double variable
printf(" \nEnter a double value: ");
// reading the float value
scanf("%lf", &n);
// finding the floor value of the given double value
int r = floor(n); // storing the integer value in the r variable
// printing the floor value of the given input
printf(" \nThe floor value of %lf is = %d", n, r);
return 0;
}
```

**Output:**

**Example 3: Taking integer value as input**

```
// program to implement floor() function in C
#include <stdio.h>
#include <math.h>
int main() {
int n; // declaring a integer variable
printf(" \nEnter a integer value: ");
// reading the integer value
scanf("%d", &n);
// finding the integer value of the given integer value
int r = floor(n); // storing the integer value in the r variable
// printing the floor value of the given input
printf(" \nThe floor value of %d is = %d", n, r);
return 0;
}
```

**Output:**

If we observe the output in the case of an integer, we get the same value as the output. There won't be any change in the given input and output.

**Example 4: Finding the fractional part using floor() function.**

The fractional part of a number is the part after the decimal point in a number. We can get that number by subtracting the floor value from the original number. And always, the original number will be greater than the floor value.

```
// calculating the fractional part using the floor() function
#include <stdio.h>
#include <math.h>
int main() {
// initializing a float value
float num;
printf(" \nEnter a floating point number: ");
// reading the num value from the keyboard
scanf("%f", &num);
// calculating the fractional value by subtracting the floor value
// from original value
float fractional = num - floor(num);
// printing the fractional part of the given number
printf(" \nThe Fractional part of %.4f = %.4f", num, fractional);
return 0;
}
```

**Output:**

### Conclusion

In this article, we learned about the floor() function in C. We knew the implementation of the floor() function. We had a look at the syntax of floor function. We discussed the working mechanism of the floor() function. We implemented the floor() function in different ways. And we discussed the calculation of fractional parts using the floor() function.