Horje
C cos() Function

cos() function in C programming is provided by the math.h header file and is used to calculate the cosine of an angle (x) where (x) is represented in radians. This function returns the cosine of a given angle value in radians for a right-angled triangle.

Syntax of cos()

double cos(double x);

Parameters of cos()

The cos(x) function takes a single parameter x, where:

  • x: is a double value representing the angle in radians for which the cosine value is to be computed. The value of x can range from negative to positive infinity.

Return value of cos()

The cos(x) function returns the cosine of the given angle x which is passed in the form of radians.

Example of cos() in C

Input:
double radian = 0.785 // where 0.785 = π/4, π= 3.141

Output:
The cosine of 0.785 radians (45 degrees) is 0.707

The below program demonstrates how we can calculate the cosine of various angles in C.

C
// C Program to calculate the cosine of various angles in C

#include <math.h>
#include <stdio.h>

int main()
{
    // 30 degrees in radians
    double a = M_PI / 6;
    // 45 degrees in radians
    double b = M_PI / 4;
    // 60 degrees in radians
    double c = M_PI / 3;
    // 90 degrees in radians
    double d = M_PI / 2;
    // Variable to store the results
    double res;

    res = cos(a);
    printf("The Cosine of %.3lf radians (30 degrees) is "
           "%.3lf\n",
           a, res);

    res = cos(b);
    printf("The Cosine of %.3lf radians (45 degrees) is "
           "%.3lf\n",
           b, res);

    res = cos(c);
    printf("The Cosine of %.3lf radians (60 degrees) is "
           "%.3lf\n",
           c, res);

    res = cos(d);
    printf("The Cosine of %.3lf radians (90 degrees) is "
           "%.3lf\n",
           d, res);

    return 0;
}

Output

The Cosine of 0.524 radians (30 degrees) is 0.866
The Cosine of 0.785 radians (45 degrees) is 0.707
The Cosine of 1.047 radians (60 degrees) is 0.500
The Cosine of 1.571 radians (90 degrees) is 0.000

Time Complexity: O(1)
Auxiliary Space: O(1)

Explanation: In the above program M_PI is a macro constant defined in the <math.h> header which represents the mathematical constant π (pi) which is approximately equal to 3.14159265358979323846




Reffered: https://www.geeksforgeeks.org


C Language

Related
tan() Function in C tan() Function in C
C Program to Solve the 0-1 Knapsack Problem C Program to Solve the 0-1 Knapsack Problem
How to Match a Pattern in a String in C? How to Match a Pattern in a String in C?
How to Read a File Line by Line in C? How to Read a File Line by Line in C?
How to Pass a 3D Array to a Function in C? How to Pass a 3D Array to a Function in C?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20