floor in C Programming
It is a function that is declared in the math.h header file, while other comparable functions empower users to effortlessly carry out specific mathematical calculations for subsequent computations.
The floor function prompts the user for a value and then provides the largest integer less than or equal to that specific value of x.
Syntax for using floor
The return type of the function is double, accepting a numerical parameter. It may alternatively be int, float, or any other data type capable of storing numerical data. Therefore, the syntax appears as:
double floor(double arg);
Implementing floor in a C Program
Below is the code snippet demonstrating the implementation of the floor function in the C programming language:
#include <stdio.h>
#include <math.h>
int main () {
// initializing the variables in the program
// here we have taken five floating pt numbers
float realno1, realno2, realno3, realno4, realno5, realno6;
float answer, answer1;
// assigning values to the initialized variables
realno1 = 3.1;
realno2 = 9.8;
realno3 = 11.9;
realno4 = 12.1;
realno5 = 16.5;
realno6 = 11.1;
//Computing and printing the floor value of the integers
printf("floor value of realno1 is = %.1lf\n", floor(realno1));
printf("floor value of realno2 is = %.1lf\n", floor(realno2));
printf("floor value of realno3 is = %.1lf\n", floor(realno3));
printf("floor value of realno4 is = %.1lf\n", floor(realno4));
printf("floor value of realno5 is = %.1lf\n", floor(realno5));
//You can either directly call the floor() function in the print statement
// or you can use it as any other function and call it outside the print and store the result in other variable
answer = floor(realno6);
printf("floor value of realno6 is = %.1f\n", answer);
//You can directly use a numerical value too
answer1 = floor(9.99);
printf("floor value of value is = %.1f\n", answer1);
return(0);
}
Output:
[Program Output]
Explanation:
We have employed the function in three distinct manners within the aforementioned program.
We initially outputted the floor result directly by utilizing the printf function, without the need to store the result in a variable.
In the second technique, we store the calculated value in the floor variable. Subsequently, we utilize the stored value from the variable to display the output.
In the previous function, a numeric value was passed as an input parameter directly, and then assigned to a variable for later printing.