×

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all integers as well as all fractions. Any integer can be written as a quotient using 1 as the numerator and an integer as the denominator.

 In this way, regular numbers appeal to ratings between the numbers, for example. There are 3,850 between 3 and 4. In some situations, we need to accept integer values ??as answers instead of getting rational numbers as answers. Therefore, floor and ceiling functions are used in such cases.

What is Ceil Function?

The ceiling is the ceiling of our house. As a result, you can see some similarities here, in other words. The Ceil function returns an integer slightly larger than some rational value.

The Ceil function is used when an exact integer value greater than or equal to the specified value is required.

For example, an upper bound of 3.138 is equivalent to 4. Note that this does not return the rounding value of 3. This is 4. It just returns an integer greater than a predetermined rational number.

C's ceil function accepts a single or double parameter as a data type for its parameters.

If the program provides a different data type, such as an int, float, long, long int, or othertypes argument will be typecast to a double type using the function's specified prototype.

The compile-time error will be thrown if any of the provided data types, such as strings, cannot implicitly typecast to double.

How Can the Ceil Function Be Used in C?

The ceil function is easy to use. Follow these three steps: determine the parameter's range or data type.

Depending on the parameter’s data type, select the appropriate function from ceil, ceill, or ceilf. Save the result in a variable whose data type is large enough to hold the ceil() function's return value in C.

#include <stdio.h>
#include <math.h>
int main () {
   float val1, val2, val3, val4;
   val1 = 1.4;
   val2 = 1.8;
   val3 = 2.7;
   val4 = 2.2;
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
   return  0;   }

Output:

value1 = 2.0
value2 = 2.0
value3 = 3.0
value4 = 3.0

Program To Ceil The Floating Number Using The ceil() Function in C

#include<stdio.h>
//This math header file inclusion is necessary to use ceil function 
#include<math.h>
int main(){
    //Declaring Variables one for storing input and passing as a parameter and the other for storing result
    float num;
    int result;
    //Input of float
printf("Enter any number: ");
scanf("%f", &num);
    //The result of ceilf, which is a float value, we are storing it in int variable so type casting will occur
    result = ceilf(num);
    //Printing the result 
printf("The ceil of the given float number is: %d", result);
    return 0;
}

Output:

Enter any number: 12.4
The ceil of the given float number is: 12

Any number can be made equal to the closest integral value by rounding it off; if the number is an integer, that could be greater than, smaller than, or the same as the number.

Using the round-off method, we check whether the fractional value written after the decimal point is greater than 0.5.

If this number is true, it is closer to the next integral value. Otherwise, 65.78 are closer to 66 than it is to the same integral value. Ex- 65.34 is similar to 65.

Therefore, we can subtract 0.5 from the number to round it off using the ceil function. Let's look at the reasoning behind this: If the fractional value written after the decimal point is greater than 0.5, then even if we subtract 0.5, some fractional part will remain. For instance, if 65.78 - 0.5 = 65.2865.780.5=65.28, the ceil function for 65.28 will return 66, roughly equivalent to 65.78.

Otherwise, if the fractional value written after the decimal point is less than 0.5 and its integral value is less than the original by subtracting 0.5 from it, then, For instance, 65.34 - 0.5 = 64.8465.340.5=64.84, and the ceil function will now return 65, which is approximately 65.34.

Otherwise, a further condition could be that if the fractional value after the decimal point equals 0.5, subtracting 0.5 will turn it into an integral value. Ex- 65.5 - 0.5 = 65.065.50.5=65.0. Even though according to our method, 66 and 65 are close to 65.5, the ceil function will return precisely that integral value in this case.

What is Floor Function?

The floor function will return an integer lower than a certain rational value. When exact integer values that are either less than or equal to the given value are required, the floor function is used.

The ceiling value of 3.883, for instance, is 3.Note that this does not return the round-off value of 4, which is 4. It simply returns an integer slightly less than a particular rational value.

Parameters of the C floor() Function:

The C floor()  function has onlyone parameter. That is, floating point numbers. The return value of floor() in C is an integer just below or equal to the specified floating-point number (a float or double value). Floating-point numbers have one decimal place, just like the C float and double functions.

// Using floor() function
#include <stdio.h>
#include <math.h>
int main() {
    // initializing a float value
    float num = 4.35;
    // returning the floor value of a float value
    int result = floor(num);
    // printing the floor value
printf("Floor integer of %.2f = %d", num, result);
    return 0;
}

Output:

Floor integer of 4.35 = 4
#include <stdio.h>
#include <math.h>
int main() {
    // initializing a float value
    float num = 8.888;
    // returning the floor value of a float value
    int result = floor(num);
    // printing the floor value
printf("Floor integer of %.3f = %d", num, result);
    return 0;
}

Output:

The floor integer of 8.888 is 8

Use the floor() function to immediately round  8.888  in the previous example to a smaller integer. 8 is the conclusion. Use the header record because the floor() function is labeled.

Providing an Input Using a Double Value

// Using floor() function with scientific notation
#include <stdio.h>
#include <math.h>
int main() {
    // initializing a double value
    double num = 5.4.5677654788654567;
    // returning the floor value of a double value
    int result = floor(num);
    // printing the floor value
printf("Floor integer of %2.27f = %d", num, result);
    return 0;
}

Output:

The floor integer of 4.5677654788654567 is 4

Using the floor() function, we rounded the double 4.5677654788654567 to its immediately smaller integer in the preceding example. We get 4 as a result of the output.

Scientific Notation (float) as Input:

// Using floor() function with scientific notation
#include <stdio.h>
#include <math.h>
int main() {
    // initializing a float value using the scientific notation
    float num = 5.678765e5;
    // returning the floor value using the floor() function
    int result = floor(num);
    // printing the floor value
printf("Floor integer of %.2f = %d", num, result);
    return 0;
}

Output:

Floor integer of 5.678765e5 = 5

Numbers that are too large or too small to be represented in decimalcan be represented using scientific notation.

Example: 0.00000000051 is written as 5.1*10-104.31010 in scientific notation. The previous example uses the floor() function to roundthe scientific notation 5.67876545678543 to its next smaller integer. This notation is primarily used for floating point numbers (float ordouble values). The result is 6376 in the output.

Use the floor() function to find the fraction of a float or double number. A fraction of a number is the number of digits after the decimal point. The difference between the original number and its largest integer is the same.

 In mathematics, the largest integer (lower bound) fraction of xx is represented by [x][x]. As a result, x = [x] + [x] + [x]

// Using floor() function to find the fractional part of a number
#include <stdio.h>
#include <math.h>
int main() {
    // initializing a float value
    float num = 5.8760;
    // getting the fractional part of num using the floor() function (fractional part = original number - floor value)
    float frac = num - floor(num);
    // printing the fractional part of num
printf("Fractional part of %.4f = %.4f", num, frac);
    return 0;
}

Output:

A fractional part of 5.8760 = 0.8760

Since the fraction of a number is equal to the difference between that number and its minimum value, we can use the floor() function to find the fraction of the number 5.8760 in the previous example. The fraction of 5.8760 here is 0.876, or 5.876 minus 5

The math.h> header file contains the floor() function definition.

Floating point numbers (float and double values) are rounded to the nearest smaller or equal integer by this function.

The floor() function  also works with integers but returns the input as-is.


Related Topics

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

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.

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.

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.

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.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

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.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.