ceil() – Ceiling Function
The ceil() function returns the smallest integer value greater than or equal to the given number. It’s useful when you need to round a number up to the nearest integer.​
Syntax:
#include <math.h> double ceil(double x);
- x: The number to be rounded up.​
- Return Value: Returns the smallest integer greater than or equal to x, as a double.​​
Example:
#include <stdio.h> #include <math.h> int main() { double score = 79.3; printf("Rounded-Up Score: %.1f\n", ceil(score)); return 0; }
Output:
Rounded-Up Score: 80.0
This C program rounds a given score (79.3) up to the nearest whole number using the ceil() function from the math library. The ceil() function always rounds a decimal number up. So, 79.3 becomes 80.0, and the result is printed as the rounded-up score.
Floor Function in C
The floor() function returns the largest integer value less than or equal to the given number. It’s used to round a number down to the nearest integer.​
Syntax:
double floor(double x);
Example:
#include <stdio.h> #include <math.h> int main() { double mcqScore = 64.9; printf("Floor Score: %.1f\n", floor(mcqScore)); return 0; }
Output:
Floor Score: 64.0
This C program uses the floor() function to round a decimal score down to the nearest whole number. The score 64.9 is passed to floor(mcqScore), which returns 64.0 by cutting off the decimal part. This result is then printed as the “Floor Score”.
Absolute Value Functions in C
The absolute value of a number is its distance from zero, regardless of its sign. In C, we use abs() for integers and fabs() for floating-point numbers.
Syntax:
#include <stdlib.h> // for abs int abs(int x); #include <math.h> // for fabs double fabs(double x);
abs() Example:
#include <stdio.h> #include <stdlib.h> int main() { int offset = -42; printf("Score Offset: %d\n", abs(offset)); return 0; }
Output:
Score Offset: 42
This C program shows how to turn a negative number into a positive one using the abs() function. It starts with a value of -42 and uses abs(offset) to get 42. This makes it easy to handle scores or values that should always be positive.
fabs() Example:
#include <stdio.h> #include <math.h> int main() { double delta = -5.7; printf("Absolute Error: %.1f\n", fabs(delta)); return 0; }
Output:
Absolute Error: 5.7
This C program uses the fabs() function to find the absolute value of a floating-point number. It starts with -5.7 and prints 5.7 as the absolute error. This is useful when you need to ignore the sign and focus on the size of a number.
Remainder Functions (fmod()) in C
The fmod() function returns the remainder of the division of two floating-point numbers. It’s useful when you need to find the modulus of non-integer values.​
Syntax:
double fmod(double x, double y);
Example:
#include <stdio.h> #include <math.h> int main() { double totalMarks = 95.5, sectionMarks = 30.0; printf("Marks Leftover: %.2f\n", fmod(totalMarks, sectionMarks)); return 0; }
Output:
Marks Leftover: 5.50
This C program uses the fmod() function to calculate the remainder when one floating-point number is divided by another. It divides 95.5 by 30.0 and prints the leftover marks, which is 5.5. This is useful for tracking what’s left after full divisions, like leftover marks or time.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for C Internship