C library - sin() function



The C library sin() function of type double accept the parameter as variable(x) that returns the sine of a radian angle. This function calculate the sine value of any angle.

A sine is a trigonometric function which is used for finding the sides of a right triangle or unknown angle.

Syntax

Following is the syntax of the C library sin() function −

sin(double x)

Parameters

It accepts only a single parameter −

  • x − This is the floating point value representing an angle expressed in radians.

Return Value

This function returns sine value of x.

Example 1

Following is the basic C library example to see the demonstration of sin() function.

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

#define PI 3.14159265

int main () {
   double x, ret, val;

   x = 45.0;
   val = PI / 180;
   ret = sin(x*val);
   printf("The sine of %lf is %lf degrees", x, ret);
   
   return(0);
}

Output

The above code produces the following result −

The sine of 45.000000 degrees is 0.707107

Example 2

The program takes a user-defined angle in degrees, converts it to radians, and calculate the sine value using sin().

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

#define PI 3.141592654

int main() {
   double angle_degrees = 135.0;
   double angle_radians = (angle_degrees * PI) / 180.0;
   double result = sin(angle_radians);

   // show the result
   printf("Sine of %.2lf degrees = %.2lf\n", angle_degrees, result);
   return 0;
}

Output

On execution of above code, we get the following result −

Sine of 135.00 degrees = 0.71

Example 3

Below the program generate the sine values ranges from 0(deg) to 100(deg) where it shows the usage of sin() in loop.

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

#define PI 3.141592654

int main() {
   printf("The sine values ranges between 0-100(deg):\n");
   for (int angle_degrees = 0; angle_degrees <= 100; angle_degrees += 10) {
       double angle_radians = (angle_degrees * PI) / 180.0;
       double result = sin(angle_radians);
       printf("sin(%d degrees) = %.2lf\n", angle_degrees, result);
    }
    return 0;
}

Output

After executing the above code, we get the following result −

The sine values ranges between 0-100(deg):
sin(0.00 degrees) = 0.00
sin(0.17 degrees) = -0.00
sin(0.34 degrees) = -0.00
sin(0.50 degrees) = -0.00
sin(0.64 degrees) = -0.00
sin(0.77 degrees) = -0.00
sin(0.87 degrees) = -0.00
sin(0.94 degrees) = 0.00
sin(0.98 degrees) = -0.00
sin(1.00 degrees) = -0.00
sin(0.98 degrees) = -0.00
Advertisements