Compute Sine of an Angle in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the sine of an angle in C programming. The lab covers the essential steps required to read an angle in radians, use the sin() function from the math.h library to calculate the sine value, and print the result. This lab aims to provide a practical understanding of trigonometric calculations using C, a fundamental skill in various scientific and engineering applications.

The lab begins by guiding you through the process of reading an angle in radians, which is the standard unit for angle measurements in mathematical computations. You will then learn how to use the sin() function from the math.h library to calculate the sine value of the given angle. Finally, you will practice printing the calculated sine value to the console.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435153{{"`Compute Sine of an Angle in C`"}} c/user_input -.-> lab-435153{{"`Compute Sine of an Angle in C`"}} c/math_functions -.-> lab-435153{{"`Compute Sine of an Angle in C`"}} end

Read Angle in Radians

In this step, you will learn how to read an angle in radians for trigonometric calculations in C programming. Radians are the standard unit for angle measurements in mathematical computations.

First, create a new C source file to implement angle input:

cd ~/project
nano sine_calculation.c

Now, add the following code to the file:

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

int main() {
    double angle_radians;

    printf("Enter an angle in radians: ");
    scanf("%lf", &angle_radians);

    printf("Angle entered: %f radians\n", angle_radians);

    return 0;
}

Compile the program with the math library:

gcc sine_calculation.c -o sine_calculation -lm

Example of running the program:

./sine_calculation

Example output:

Enter an angle in radians: 1.57
Angle entered: 1.570000 radians

Use sin() Function from math.h

In this step, you will learn how to use the sin() function from the math.h library to calculate the sine of an angle in radians.

Open the previously created file and modify the code to calculate the sine value:

nano ~/project/sine_calculation.c

Update the code with the following implementation:

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

int main() {
    double angle_radians;

    printf("Enter an angle in radians: ");
    scanf("%lf", &angle_radians);

    double sine_value = sin(angle_radians);

    printf("Angle: %f radians\n", angle_radians);
    printf("Sine of the angle: %f\n", sine_value);

    return 0;
}

Compile the program with the math library:

gcc sine_calculation.c -o sine_calculation -lm

Run the program and test with different angle values:

./sine_calculation

Example output:

Enter an angle in radians: 1.57
Angle: 1.570000 radians
Sine of the angle: 1.000000

Example output for another angle:

Enter an angle in radians: 0.785
Angle: 0.785000 radians
Sine of the angle: 0.707107

Print the Sine Value

In this final step, you will enhance the sine calculation program to provide more detailed output and demonstrate different formatting options for printing sine values.

Open the existing file to make final modifications:

nano ~/project/sine_calculation.c

Update the code with advanced printing techniques:

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

int main() {
    double angle_radians;

    printf("Enter an angle in radians: ");
    scanf("%lf", &angle_radians);

    double sine_value = sin(angle_radians);

    // Different formatting options for printing
    printf("Angle Entered: %.2f radians\n", angle_radians);
    printf("Sine Value (default): %f\n", sine_value);
    printf("Sine Value (scientific): %e\n", sine_value);
    printf("Sine Value (precision): %.4f\n", sine_value);

    return 0;
}

Compile the program:

gcc sine_calculation.c -o sine_calculation -lm

Run the program:

./sine_calculation

Example output:

Enter an angle in radians: 1.57
Angle Entered: 1.57 radians
Sine Value (default): 1.000000
Sine Value (scientific): 1.000000e+00
Sine Value (precision): 1.0000

Summary

In this lab, you learned how to read an angle in radians and use the sin() function from the math.h library to calculate the sine of the angle. First, you created a C program that prompts the user to enter an angle in radians, and then you printed the entered angle. Next, you updated the program to calculate the sine value using the sin() function and print the result. The lab provided step-by-step instructions and example outputs to guide you through the process.

Other C Tutorials you may like