Ceil Function In C

Syntax

Example

double ceil ( double x);

Parameters

Here, x is an argument of the ceil function.

Returns

The ceil function provides the nearest higher integer value for the given number x when rounded up.

Properties of the ceil function

  • It is a built-in method of math.h header file.
  • Using the ceil function to return the smallest number greater than or equal to the parametric number.
  • If the passed number is 0, the ceil function returns the 0.
  • If the number is equal to the mathematical integer, the result generated by the ceil function will be the same as the specified number.
  • If a number is less than 0 but greater than -1.0, the result will be zero.
  • Note: The ceil function can only be used with floating-point positive and negative numbers to round off the given number. If the passed positive and negative number is an integer, the function returns the same number without ceiling or round off the nearest given number.

    Example 1: Program to ceil a number using the ceil function

    Example
    
    #include <stdio.h>
    #include <math.h>
    int main ()
    {
    // declare local variables
    double num = 9.37;
    int result;
    
    // use ceil () function to round up the nearest value. 
    result = ceil (num);
    
    printf ( " \n The ceiling integer of %.2f = %d ", num, result);
    return 0;	
    }
    

Output

Output

The ceiling integer of 9.37 = 10

As demonstrated in the code snippet, when the variable num with a value of 9.37 is supplied to the ceil function, it yields the rounded-up integer value of 10.

Example 2: Program to ceil the floating number using the ceil function

Example

#include <stdio.h>
#include <math.h>
int main ()
{
// declare local variables
float f1, f2, f3, f4, f5;
f1 = 2.3;
f2 = -2.7;
f3 = 5.1;
f4 = 56.4;
f5 = 2.8;

// use ceil() function to return the nearest number
printf (" The ceiling value of %.2f is %.2f \n ", f1, ceil (f1));
printf (" The ceiling value of %.2f is %.2f \n ", f2, ceil (f2));
printf (" The ceiling value of %.2f is %.2f \n ", f3, ceil (f3));
printf (" The ceiling value of %.2f is %.2f \n ", f4, ceil (f4));
printf (" The ceiling value of %.2f is %.2f \n ", f5, ceil (f5));
return 0;
}

Output

Output

The ceiling value of 2.30 is 3.00
  The ceiling value of -2.70 is -2.00
  The ceiling value of 5.10 is 6.00
  The ceiling value of 56.40 is 57.00
  The ceiling value of 2.80 is 3.00

Example 3: Program to round up the floating number using the ceil function

Example

#include <stdio.h>
#include <math.h>
int main ()
{
// declare local variables
float num;
float result;

printf (" Enter the float number: ");
scanf ( "%f", &num); 

// use ceil () function
result = ceil (num);

printf ( " The ceil of %.2f is %f ", num, result);
return 0;
}

Output

Output

Enter the float number: 55.6
 The ceil of 55.60 is 56.000000

Example 4: Program to round up the positive and negative numbers using the ceil function

Example

#include <stdio.h>
#include <math.h>
int main ()
{

int num, result, num2, result2;

printf (" Enter the positive integer: ");
scanf (" %d", &num);

printf (" Enter the negative integer: ");
scanf (" %d", &num2);

// use ceil () function to round up the nearest value of the given number. */
result = ceil (num);
result2 = ceil (num2);

printf ( " \n The ceiling integer of %d = %d ", num, result);
printf ( " \n The ceiling integer of %d = %d ", num2, result2);
return 0;
}

Output

Output

Enter the positive integer: 56
 Enter the negative integer: -24

 The ceiling integer of 56 = 56
 The ceiling integer of -24 = -24

Input Required

This code uses input(). Please provide values below: