C floor()

The floor() function defined in the math.h header file. It helps to return the nearest integer value less than or equal to the given argument value.


double floor(double x) #where x should be in double

Also, two functions floorf() and floorl() were used with type float and long double respectively.


float floorf(float x); 
long double floorl(long double x); 

floor() Parameters:

The floor() function takes a single parameter in double.

Parameter Description Required / Optional
double value whose lesser nearest integer needs to be found Required

floor() Return Value

The return value of floor() function is a number in the integer.

Input Return Value
double value integer

Examples of floor()

Example 1: Working of floor() function in C?


#include <stdio.h>
#include <math.h>
int main()
{
   double N = 4.82;
   int ouput;

   ouput = floor(N);
   printf("Floor integer of  given %.2f = %d", N, ouput);

   return 0;
}

Output:


Floor integer of given 4.82 = 4
 

Example 2: How floor() function worked in C?


#include <stdio.h>
#include <math.h>
int main () {
   float v1, v2, v3, v4;

   v1 = 3.5;
   v2 = 4.8;
   v3 = 5.3;
   v4 = 6.7;

   printf ("value1 = %.1lf\n", floor(v1));
   printf ("value2 = %.1lf\n", floor(v2));
   printf ("value3 = %.1lf\n", floor(v3));
   printf ("value4 = %.1lf\n", floor(v4));
   
   return(0);
}

Output:


value1 = 3.0
value2 = 4.0
value3 = 5.0
value4 = 6.0