Examples
Ceil a number using the ceil function
Code
#include <stdio.h>
#include <math.h>
int main ()
{
double a = 14.70;
int ans;
// round up to nearest value
ans = ceil (a);
printf ( " \n The ceiling integer of given %.2f is %d ", a, ans);
return 0;
}
Output
The ceiling integer of given 14.70 is 15
Ceil a floating number using ceil function
Example
#include <stdio.h>
#include <math.h>
int main ()
{
float x1=12.33, x2=-41.67;
float ans1=ceil(x1), ans2=ceil(x2);
printf ("The ceiling value of positive floating number %.2f is %.2f \n", x1,ans1);
printf ("The ceiling value of negative floating number %.2f is %.2f \n ", x2, ans2);
return 0;
}
Output
The ceiling value of positive floating number 12.33 is 13.00
The ceiling value of negative floating number -41.67 is -41.00
You can practice by yourself with the help of online c compiler for good practice.
Must Read Passing Arrays to Function in C
Characteristics of Ceil function
- It is a predefined/built-in function available in math.h header file.
- The function returns the smallest number that is just greater than or equal to the number.
- The ceil function will return 0 if we pass 0 as an argument.
- If the number which is passed as an argument is equal to the mathematical integer, then the result of the ceil function will be the same as the number passed as an argument.
-
Any number which is less than 0 but greater than -1.0, will result in 0.
Also see, Tribonacci Series and Short int in C Programming
Must Read Decision Making in C
FAQs
-
What is the difference between ceil and floor function?
A ceil function returns the smallest integer greater than or equal to the given number, whereas the floor function returns the largest number smaller than or equal to the given number.
-
Name a few functions which are present in the math.h header file.
Functions like sqrt(number): returns the square root of given number, pow(base, exponent): returns the power of given number, abs(number): returns the absolute value of given number, etc.
-
What will be the output of the ceil function if we pass 0 as the argument.
The ceil function will return 0, if we pass 0 as the argument.
Key Takeaways
In this article, we have extensively discussed what ceil function is, how to use it, and its implementation in Visual Studio Code.
Also read reverse a number.
We hope that this blog has helped you enhance your knowledge regarding the Ceiling function, and if you would like to learn more, check out our articles on different functions in STL, how to implement graphs using STL, understanding type casting, and type conversion in C++, etc.
Do upvote our blog to help other ninjas grow. Happy Coding!”