×

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

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

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.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.