The C cos Function is a Math Library Function, used to calculate the Trigonometry Cosine value for the specified expression. The syntax of the cos is
double cos(double number);
The COS function in C will return the value between -1 and 1. Before we get into to the syntax, let us see the mathematical formula behind this C Trigonometry Cosine function:
cos(x) = Length of the Adjacent Side / Length of the Hypotenuse
COS Function in C Example
The cos function in the math library allows you to find the trigonometry Cosine for the specified values.
This C program, ask the user to enter his/her own value, and then it will find the cosine value of the user-specified one
TIP : Please refer C ACOS Function article in C Programming to calculate the Arc Cosine of the specified expression.
#include <stdio.h> #include <math.h> int main() { double cosValue, number; printf(" Please Enter the Value to calculate Cosine : "); scanf("%lf", &number); cosValue = cos(number); printf("\n Cosine value of %lf = %f ", number, cosValue); return 0; }
C COSINE Function Example 2
In this C cosine function example, we are allowing the user to enter the value in degrees. Then we are converting the degrees to Radians. And finally, we are finding the cosine value of the radian.
#include <stdio.h> #include <math.h> #define PI 3.14 int main() { double cosValue, radianVal, degreeVal; printf(" Please Enter an Angle in degrees : "); scanf("%lf", °reeVal); // Convert Dgree Value to Radian radianVal = degreeVal * (PI/180); cosValue = cos(radianVal); printf("\n Cosine value of %f = %f ", degreeVal, cosValue); return 0; }
Please Enter an Angle in degrees : 35
Cosine value of 35.000000 = 0.819330