C++

# How to Use sin() Function in C++ with Example

The sin() is a mathematical function that calculates the sine of an angle. The sin() function can be used to determine the sine of an angle in radians in C++. It is a component of the cmath library. In this tutorial, we will analyze the sin() in C++ and demonstrate it using examples of C++ programming language.

## What is sin() in C++?

In C++ the sine of an angle is found using the sin() function, a library function in the cmath header. It takes an integer (num) and outputs the sine of an angle of num radians, as shown in the syntax below:

sin(num);

Parameters: The num variable is passed to the sin function that represents the radian value of the angle whose sine has to be determined.

Return Value: The sin() function returns a double data type.

Now, moving towards the examples of C++ that implement sin().

## Example 1: Finding the Sine of an Angle using sin() Function

Below is a simple example of a sin() function that finds the angle of a given value in radians:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double my_angle = 270; // angle represent in degrees
double inRadian = my_angle * M_PI / 180; // angle converted to radians
cout << "The sin of " << my_angle << " degrees is " << num<< endl;
return 0;
}

This example starts by declaring an integer my_angle and setting its value to 270. The angle is then multiplied by M_PI/180.0, where M_PI is a constant provided in the cmath toolkit that represents the value of pi, to obtain the angle’s radian value. The sine of the angle is then calculated by passing the angle’s radian_formula to the sin function. Finally, we save the outcome in a num variable and output it to the console by using cout. The output from running this program is as follows:

## Example 2: Find the Sine of an Angle by Taking User Input using the sin() Function

The following program calculates the different sine values of the integer type variables:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double my_angle;
cin>>my_angle;
double inRadians = my_angle * M_PI / 180;
cout<<"The value of sin("<<my_angle<<") is: "<< sin(inRadians) <<endl;
return 0;
}

The above program is taking input from the user and then converts the value to radians. After that the sin() function is applied to that value.

## Conclusion

The C++ sin() function is a helpful tool for finding the sine value of angles specified in radians or degrees. Numerous fields, including trigonometry, physics, and engineering, can benefit from the use of the sin() function. This article provided two examples to use the C++ sin() function.