Mathematical functions
In C Language we have a number of inbuilt Mathematical functions which help us to perform various operations.
Header file: math.h
abs():
Helps us to find the absolute value of an integer value
labs():
Helps us to find the absolute value of a value of type long.
fabs() or fabsl():
Helps us to find the absolute value of a value of type float.
#include <stdio.h> #include<math.h> int main() { int n1 = -1234; float n2 = -1234.0; long n3,x = -12345678L; int n; printf("(int value) number: %d absolute value: %d\n",n1,abs(n1)); printf("Enter any no "); scanf("%d",&n); printf("(int) abs value of %d is %d\n",n,abs(n)); //long n3= labs(x); printf("(long value)number: %ld abs value: %ld\n", x, n3); //float value printf("(float value)number:%f abs value: %f\n",n2,fabs(n2)); return 0; }
/* Output */ (int value) number: -1234 absolute value: 1234 Enter any no -65 (int) abs value of -65 is 65 (long value)number: -12345678 abs value: 12345678 (float value)number:-1234.000000 abs value: 1234.000000
ceil(x):
Helps us to find upper side nearest integer. i.e. finds the smallest integer not < x.
floor(x):
Helps us to find lower side nearest integer i.e. finds the largest integer not > x.
#include <stdio.h> #include<math.h> int main() { double n = 123.54; printf("number: %5.2lf ceil: %5.2lf Floor: %5.2lf\n", n,ceil(n),floor(n)); n=-123.54; printf("number: %5.2lf ceil: %5.2lf Floor: %5.2lf\n", n,ceil(n),floor(n)); return 0; }
/* Output */ number: 123.54 ceil: 124.00 Floor: 123.00 number: -123.54 ceil: -123.00 Floor: -124.00
modf():
breaks the double x into two parts: the integer and the fraction. It
modfl():
modfl is the long double version of modf.
#include <stdio.h> #include<math.h> int main() { double fraction, integer; double number = 100.56; fraction = modf(number, &integer); printf("The whole and fractional parts of \n%lf are %lf and %lf\n",number, integer, fraction); return 0; }
/* Output */ The whole and fractional parts of 100.560000 are 100.000000 and 0.560000