C program to find out cosine and sine values using math.h library.

In C programming, the math.h library provides trigonometric functions like cos() and sin() to calculate cosine and sine values. These functions take angles in radians as input and return the corresponding trigonometric values.

Syntax

#include <math.h>

double cos(double x);   // Returns cosine of x (in radians)
double sin(double x);   // Returns sine of x (in radians)

Parameters

  • x − The angle in radians for which to calculate the trigonometric value

Return Value

Both functions return a double value representing the cosine or sine of the input angle.

Note: To compile programs using math.h functions, link with the math library using -lm flag: gcc program.c -lm

Example 1: Finding Cosine Values

This program calculates cosine values for angles from 0 to 150 degrees in steps of 10 degrees −

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

#define PI 3.14159
#define MAX 150

int main() {
    int angle;
    double x, y;
    
    printf("Angle     cos(angle)<br><br>");
    
    for(angle = 0; angle <= MAX; angle += 10) {
        x = (PI / 180.0) * angle;  /* Convert degrees to radians */
        y = cos(x);
        printf("%5d %13.4f<br>", angle, y);
    }
    
    return 0;
}
Angle     cos(angle)

    0        1.0000
   10        0.9848
   20        0.9397
   30        0.8660
   40        0.7660
   50        0.6428
   60        0.5000
   70        0.3420
   80        0.1736
   90        0.0000
  100       -0.1736
  110       -0.3420
  120       -0.5000
  130       -0.6428
  140       -0.7660
  150       -0.8660

Example 2: Finding Sine Values

This program calculates sine values for the same range of angles −

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

#define PI 3.14159
#define MAX 150

int main() {
    int angle;
    double x, y;
    
    printf("Angle     sin(angle)<br><br>");
    
    for(angle = 0; angle <= MAX; angle += 10) {
        x = (PI / 180.0) * angle;  /* Convert degrees to radians */
        y = sin(x);
        printf("%5d %13.4f<br>", angle, y);
    }
    
    return 0;
}
Angle     sin(angle)

    0        0.0000
   10        0.1736
   20        0.3420
   30        0.5000
   40        0.6428
   50        0.7660
   60        0.8660
   70        0.9397
   80        0.9848
   90        1.0000
  100        0.9848
  110        0.9397
  120        0.8660
  130        0.7660
  140        0.6428
  150        0.5000

Key Points

  • The cos() and sin() functions expect angles in radians, not degrees
  • To convert degrees to radians: radians = (PI / 180.0) * degrees
  • Include math.h header and link with math library using -lm flag
  • Use double for better precision in trigonometric calculations

Conclusion

The math.h library in C provides reliable trigonometric functions for calculating sine and cosine values. Remember to convert degrees to radians for accurate results and link the math library during compilation.

Updated on: 2026-03-15T13:59:41+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements