sin() Function Example in C Programming

In this source code example, we will see how to use the sin() function in C programming with an example.

sin() Function Overview

The sin() function in C is used to calculate the sine of a given angle in radians. It is part of the math.h library. This function takes a single argument, the angle in radians, and returns the sine value of the given angle. 

 Key Points: 

- The function requires math.h to be included. 

- The argument should be in radians. To convert degrees to radians, you can use the formula: radian = degree × π / 180.

 - Always link the math library during compilation using -lm flag.

Source Code Example

#include <stdio.h>
#include <math.h> // Required for sin()

int main() {
    double angle_in_degrees, angle_in_radians, result;

    // Input angle in degrees
    printf("Enter angle in degrees: ");
    scanf("%lf", &angle_in_degrees);

    // Convert degrees to radians
    angle_in_radians = angle_in_degrees * M_PI / 180.0;

    // Calculate the sine value
    result = sin(angle_in_radians);

    // Display the result
    printf("Sine value of %.2lf degrees is: %.2lf\n", angle_in_degrees, result);

    return 0;
}

Output

Enter angle in degrees: 30
Sine value of 30.00 degrees is: 0.50

Explanation

1. We first include the required header files: stdio.h for input/output functions and math.h for mathematical functions including sin().

2. In our main() function, we take an input for angle in degrees.

3. The angle is then converted from degrees to radians using the formula: radian = degree × π / 180. In C, the constant M_PI is defined in math.h and represents the value of π.

4. We then call the sin() function with the radian value as its argument to get the sine value.

5. Finally, the result is printed to the console.


Comments