sin() Function

The sin() function computes the sine of an angle expressed in radians. It is a part of the standard math librarys.


Syntax of sin()

</>
Copy
double sin(double x);
float sinf(float x);
long double sinl(long double x);

Parameters

ParameterDescription
xAn angle in radians. (One radian is equivalent to 180/PI degrees.)

It is important to note that the function expects the angle to be in radians, so any degree measure must be converted to radians before using sin(). The function is implemented to return the sine of the provided angle.

Return Value

The function returns the sine of the given angle, computed as a double, float, or long double depending on the version of the function used.


Examples for sin()

Example 1: Calculating the Sine of PI/2

This example demonstrates how to use sin() to compute the sine of PI/2 radians, which should return 1.0.

Program

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

#define PI 3.14159265358979323846

int main() {
    double angle = PI / 2;
    double result = sin(angle);
    
    printf("sin(PI/2) = %f\n", result);
    return 0;
}

Explanation:

  1. An angle of PI/2 radians is computed.
  2. The sin() function is called with this angle.
  3. The result, which is the sine of PI/2, is stored in the variable result.
  4. The value is printed to the console.

Program Output:

sin(PI/2) = 1.000000

Example 2: Calculating the Sine of a User-Defined Angle

This example shows how to calculate the sine of an angle entered by the user (in radians) and display the result.

Program

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

int main() {
    double angle, result;
    
    printf("Enter an angle in radians: ");
    scanf("%lf", &angle);
    
    result = sin(angle);
    printf("sin(%f) = %f\n", angle, result);
    return 0;
}

Explanation:

  1. The program prompts the user to enter an angle in radians.
  2. The entered angle is read and stored in the variable angle.
  3. The sin() function calculates the sine of the provided angle.
  4. The result is printed to the console.

Program Output:

Enter an angle in radians: 1.570796
sin(1.570796) = 1.000000

Example 3: Demonstrating Sine of a Negative Angle

This example demonstrates the behavior of the sin() function when given a negative angle.

Program

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

int main() {
    double angle = -1.0;
    double result = sin(angle);
    
    printf("sin(-1.0) = %f\n", result);
    return 0;
}

Explanation:

  1. A negative angle (-1.0 radians) is set.
  2. The sin() function computes the sine of the negative angle.
  3. The result, which will be negative, is stored in the variable result.
  4. The outcome is printed to the console.

Program Output:

sin(-1.0) = -0.841471

Example 4: Using sin() to Compute Sine for Multiple Angles

This example demonstrates the use of the sin() function in a loop to compute the sine values for a series of angles.

Program

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

int main() {
    double angle;
    for (int i = 0; i <= 5; i++) {
        angle = i * (3.14159265 / 10); // Incremental angles in radians
        printf("sin(%f) = %f\n", angle, sin(angle));
    }
    return 0;
}

Explanation:

  1. A loop iterates over a range of values to calculate different angles in radians.
  2. For each angle, the sin() function computes the corresponding sine value.
  3. The result for each angle is printed to the console.

Program Output:

sin(0.000000) = 0.000000
sin(0.314159) = 0.309017
sin(0.628319) = 0.587785
sin(0.942478) = 0.809017
sin(1.256637) = 0.951057
sin(1.570796) = 1.000000