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 a 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
- cosh(a): returns the hyperbolic cos of a
- sinh(a): returns the hyperbolic sin of a
- tanh(a): returns the hyperbolic tangent of a
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
When you run this program, it will calculate the square root of 25.0 and display the result. The output will be:
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. Here, we are calculating e to the power 2.0:
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
When you run this program, it will calculate the value of e raised to the power of 2.0 and display the result. The output will be:
1 2 3 |
Exponential value of 2.000000 = 7.389056 |
cos() in C Language
The cos() function in C returns the cosine of a. We will convert the angle from degrees to radians before passing it to the cos() function.
The #define is a preprocessor directive used to create symbolic constants. It allows you to define a constant value that can be used throughout your code, making your code more readable and maintainable.
The following defines a constant named PI with the value 3.14159265, which represents the mathematical constant π (pi). By using #define, you can use PI in your code instead of repeatedly typing 3.14159265:
1 2 3 |
#define PI 3.14159265 |
Let us now see an example of the cos() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = 30.0, res; // Convert the angle from degrees to radians a = (a * PI) / 180; res = cos(a); printf("Cosine of 30.0 degrees (converted to %lf radians) = %lf\n", a, res); } |
Output
1 2 3 |
Cosine of 30.0 degrees (converted to 0.523599 radians) = 0.866025 |
sin() in C Language
The sin() function in C returns the sin of a. We have converted the angle from degrees to radians before passing it to the sin() function.
Let us now see an example of the sin() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = 60.0, res; // Convert the angle from degrees to radians a = (a * PI) / 180; res = sin(a); printf("Sine of 60.0 degrees (converted tp %lf radians) = %lf\n", a, res); } |
Output
1 2 3 |
Sine of 60.0 degrees (converted to 1.047198 radians) = 0.866025 |
tan() in C Language
The tan() function in C is used to return the tangent of a. The tan() function in C expects the angle to be in radians, not degrees. We will convert the angle from degrees to radians before using the tan() function.
Let us now see an example of the tan() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = 60.0, res; // Convert the angle from degrees to radians a = a * (PI / 180.0); res = tan(a); printf("Tan of 60 degrees = %lf\n", res); return 0; } |
Output
1 2 3 |
Tan of 60 degrees = 1.732051 |
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 15 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = -0.30, res; // Calculate arc cosine of a and convert from radians to degrees res = acos(a)*180/PI; printf("Arccosine %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Arccosine -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 15 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = -0.30, res; // Calculate arc sine of a and convert from radians to degrees res = asin(a)*180/PI; printf("Arcsine %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Arcsine -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 15 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main() { double a = -0.30, res; // Calculate arc tangent of a and convert from radians to degrees res = atan(a)*180/PI; printf("Arctangent of %lf = %lf degrees\n", a, res); } |
Output
1 2 3 |
Arctangent of -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
When you run this program, it will calculate the hyperbolic cosine of 2.5 and display the result. The output will be:
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
When you run this program, it will calculate the hyperbolic sine of 2.5 and display the result. The output will be:
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
When you run this program, it will calculate the hyperbolic tangent of 2.5 and display the result. The output will be:
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