Mathematical Functions in c programming langauage
math.h to implement mathematical operations.Here is an illustration code for mathematical functions usage:
math.h to implement mathematical operations.This program demonstrates the usage of various mathematical functions provided in the "math.h" header file of C. It initializes a variable "a" with the value 0.25 and then uses various functions such as abs(), acos(), asin(), atan(), atan2(), cos(), sin(), tan(), and sqrt() to perform mathematical operations on "a" and store the result in variable "b". Finally, it prints the value of "b" using the printf() function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
float a;
float b;
float pi = 3.14;
a = 0.25;
// Printing the value of a
printf("value a = %.2f \n", a);
// Calculating and printing the absolute value of a
b = abs(-a); // Note: abs() is for integers, fabs() should be used for floats
printf("abs(a)=%.2f \n", b);
// Calculating and printing the arc cosine of a
b = acos(a);
printf("acos(a)=%.2f \n", b);
// Calculating and printing the arc sine of a
b = asin(a);
printf("asin(a)=%.2f \n", b);
// Calculating and printing the arc tangent of a
b = atan(a);
printf("atan(a)=%.2f \n", b);
// Calculating and printing the arc tangent of a divided by 5
b = atan2(a, 5);
printf("atan(a,5)=%.2f \n", b);
// Calculating and printing the cosine of a
b = cos(a);
printf("cos(a)=%.2f \n", b);
// Calculating and printing the sine of a
b = sin(a);
printf("sin(a)=%.2f \n", b);
// Calculating and printing the tangent of a
b = tan(a);
printf("tan(a)=%.2f \n", b);
// Calculating and printing the square root of a
b = sqrt(a);
printf("sqrt(a)=%.2f \n", b);
return 0;
}
value a = 0.25
abs(a)=0.00
acos(a)=1.32
asin(a)=0.25
abs(a)=0.24
atan(a,5)=0.05
cos(a)=0.97
sin(a)=0.25
tan(a)=0.26
sqrt(a)=0.50
Press any key to continue . . .
Include Libraries: The #include directives at the beginning include the standard input-output library (stdio.h), the standard library (stdlib.h), and the math library (math.h).
Variable Declarations:
float a and float b are declared to store floating-point numbers.float pi is declared and initialized to 3.14, though it is not used in this program.Initialization and Printing:
a is initialized to 0.25.a is printed with 2 decimal places using printf.Mathematical Operations and Printing:
abs(-a) calculates the absolute value of -a. Note: abs() is used for integers, so fabs() should be used for floating-point numbers to avoid incorrect results.acos(a) calculates the arc cosine (inverse cosine) of a.asin(a) calculates the arc sine (inverse sine) of a.atan(a) calculates the arc tangent (inverse tangent) of a.atan2(a, 5) calculates the arc tangent of a divided by 5, considering the sign of both arguments.cos(a) calculates the cosine of a.sin(a) calculates the sine of a.tan(a) calculates the tangent of a.sqrt(a) calculates the square root of a.Printing Results: Each result is printed with 2 decimal places using printf.
Return Statement: The program ends with a return 0;, indicating successful execution.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.