Certainly! In C, the cos() function is part of the math library and is used to calculate the cosine of an angle. The function takes an angle in radians as input and returns the cosine of that angle as a floating-point value.
Here's an example code that demonstrates the usage of the cos() function in C:
#include <stdio.h>
#include <math.h>
int main() {
double angle = 1.2; // Angle in radians
double result = cos(angle);
printf("Cosine of %.2f radians is %.4f\n", angle, result);
return 0;
}
In this example, we include the necessary header files <stdio.h> and <math.h>. The math.h header file provides the declaration of the cos() function.
Inside the main() function, we declare a variable angle and assign it a value of 1.2 radians. You can change this value to any angle in radians that you want to calculate the cosine for.
Next, we call the cos() function and pass the angle as its argument. The result is stored in the variable result.
Finally, we use printf() to display the original angle and the computed cosine value. The format specifier %.2f specifies that we want to display the angle with 2 decimal places, and %.4f specifies that we want to display the cosine value with 4 decimal places.
When you run this code, it will calculate the cosine of the angle 1.2 radians and print the result to the console.
Note: The cos() function expects the angle to be in radians. If you have an angle in degrees, you can convert it to radians using the formula: angle_in_radians = angle_in_degrees * (PI / 180), where PI is a constant representing the value of pi (approximately 3.14159).