×

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:

Floor() Function in C

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:

Floor() Function in C

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:

Floor() Function in C

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:

Floor() Function in C

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:

Floor() Function in C

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.


Related Topics

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.