Mathematical Functions in c programming langauage
C Programming Language Function in C Language (Article) Function in C Language (Program)
1191
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.
Program:
#include #include #include 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; }
Output:
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 . . .
Explanation:
-
Include Libraries: The
#includedirectives 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 aandfloat bare declared to store floating-point numbers.float piis declared and initialized to 3.14, though it is not used in this program.
-
Initialization and Printing:
ais initialized to 0.25.- The value of
ais printed with 2 decimal places usingprintf.
-
Mathematical Operations and Printing:
abs(-a)calculates the absolute value of-a. Note:abs()is used for integers, sofabs()should be used for floating-point numbers to avoid incorrect results.acos(a)calculates the arc cosine (inverse cosine) ofa.asin(a)calculates the arc sine (inverse sine) ofa.atan(a)calculates the arc tangent (inverse tangent) ofa.atan2(a, 5)calculates the arc tangent ofadivided by 5, considering the sign of both arguments.cos(a)calculates the cosine ofa.sin(a)calculates the sine ofa.tan(a)calculates the tangent ofa.sqrt(a)calculates the square root ofa.
-
Printing Results: Each result is printed with 2 decimal places using
printf. -
Return Statement: The program ends with a
return 0;, indicating successful execution.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.