×

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

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 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.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.