21 Nov C – Math Functions
As the name suggests, the Math Functions in C language are used to perform mathematical tasks such as returning the sine, cosine, cube root, absolute value, etc. To work on Math Functions in C language, use the following header file:
1 2 3 |
math.h |
Include it in your program file:
1 2 3 |
#include <math.h> |
Here are the popular math functions in C language and what they will return after execution:
- sqrt(a): square root of a
- exp(a): e raised to the power of an i.e., Ea
- cos(a): cosine of a
- sin(a): sine of a
- tan(a): tangent of an angle
- acos(a): arccosine of a
- asin(a): arcsine of a
- atan(a): arctangent of a
- ceil(a): rounds upwards to its nearest integer
- floor(a): rounds a downwards to its nearest integer
Let us now see the above math functions one by one with examples:
sqrt() in C Language
The sqrt() function in C is used to get the square root of a number.
Let us now see an example of the sqrt() function:
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> #include <math.h> int main() { double a = 25.0; printf("Square root of %lf is %lf", a, sqrt(a)); return 0; } |
Output
1 2 3 |
Square root of 25.000000 is 5.000000 |
exp() in C Language
The exp() function in C is used to return e raised to the power of a.
Let us now see an example of the exp() function:
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> #include <math.h> int main() { double a = 2.0; printf("Exponential value of %lf = %lf\n", a, exp(a)); return 0; } |
Output
1 2 3 |
Exponential value of 2.000000 = 7.389056 |
cos() in C Language
The cos() function in C returns the cosine of a.
Let us now see an example of the cos() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = 30.0, res; a = (a * PI) / 180; res = cos(a); printf("Cosine %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Cosine 0.523599 = 0.866025 degrees |
sin() in C Language
The sin() function in C returns the sin of a.
Let us now see an example of the sin() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = 60.0, res; a = (a * PI) / 180; res = sin(a); printf("Sine %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Sine 1.047198 = 0.866025 degrees |
tan() in C Language
The tan() function in C is used to return the tangent of a.
Let us now see an example of the tan() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = 60.0, res; res = tan(a); printf("Tan %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Tan 60.000000 = 0.320040 degrees |
acos() in C Language
The acos() function in C returns the arc cosine of a in radians.
Let us now see an example of the acos() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = -0.30, res; res = acos(a)*180/PI; printf("Cosine %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Cosine -0.300000 = 107.457603 degrees |
asin() in C Language
The asin() function in C returns the arc sine of a in radians.
Let us now see an example of the asin() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = -0.30, res; res = asin(a)*180/PI; printf("Sine %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Sine -0.300000 = -17.457603 degrees |
atan() in C Language
The atan() function in C returns the arc tangent of a in radians.
Let us now see an example of the atan() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = -0.30, res; res = atan(a)*180/PI; printf("Tan %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Tan -0.300000 = -16.699244 degrees |
cosh() in C Language
The cosh() function in C returns the hyperbolic cos of a.
Let us now see an example of the cosh() function:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> #include <math.h> int main() { double a = 2.5, res; res = cosh(a); printf("Cosh %lf = %lf\n", a, res); } |
Output
1 2 3 |
Cosh 2.500000 = 6.132289 |
sinh() in C Language
The sinh() function in C returns the hyperbolic sine of a.
Let us now see an example of the sinh() function:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> #include <math.h> int main() { double a = 2.5, res; res = sinh(a); printf("Sinh %lf = %lf\n", a, res); } |
Output
1 2 3 |
Sinh 2.500000 = 6.050204 |
tanh() in C Language
The tanh() function in C returns the hyperbolic tangent of a.
Let us now see an example of the tanh() function:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> #include <math.h> int main() { double a = 2.5, res; res = tanh(a); printf("Tanh %lf = %lf\n", a, res); } |
Output
1 2 3 |
Tanh 2.500000 = 0.986614 |
If you liked the tutorial, spread the word, and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments