In C programming, we can perform various mathematical operations using built-in functions from the standard math library "math.h".
Standard Library: Math functions are part of the standard C library
Common Operations: Math functions include operations like square roots, powers, logarithms, trigonometric functions, and rounding functions.
Floating-point Arithmetic: Most math functions operate on floating-point numbers (float or double), although some functions also support integer operations.
Here are some commonly used math functions:
Absolute Value Function. Returns the absolute value of num.
#include <stdio.h> #include <math.h> int main() { double number = -10.10; double result = fabs(number); printf("Absolute value of %lf is %lf\n", number, result); return 0; }
Absolute value of -10.100000 is 10.100000
#include <stdio.h> #include <math.h> int main() { double number = 10.4; double ceil_result = ceil(number); double floor_result = floor(number); printf("Ceiling of %lf is %lf\n", number, ceil_result); printf("Floor of %lf is %lf\n", number, floor_result); return 0; }
Ceiling and Floor Functions:
ceil(num): returns the smallest integer greater than or equal to "num".
floor(num): returns the largest integer less than or equal to "num".
Ceiling of 10.400000 is 11.000000 Floor of 10.400000 is 10.000000
#include <stdio.h> #include <math.h> int main() { int base = 2; int exponent = 4; int result = pow(base, exponent); printf("%d raised to the power of %d is %d\n", base, exponent, result); return 0; }
Power Function: Returns "num1" raised to the power of "num2".
#include <stdio.h> #include <math.h> int main() { int number = 16; double result = sqrt(number); printf("Square root of %d is %lf\n", number, result); return 0; }
2 raised to the power of 4 is 16
Square Root Function: Returns the square root of "num".
Square root of 16 is 4.000000