What is sin() function
The sin() function calculates sine of the angle in C++. The function accepts a numeric as an input variable and returns the value in float, double or long double type variables.
Syntax
Example 1: Calculating sin() function with Defined Input Angle
The sin () function is called with the library of cmath in the header of the program. In this example, sin() of the angle ‘x’ of 1.57 radians is calculated:
#include<cmath>
using namespace std;
int main()
{
float x=1.57;
cout<<"sin() of an 1.57 = "<<sin(x) <<"\n\n";
return 0;
}
Here first the float variable ‘x’ has been defined for the value of angle in radians and the sine is calculated using sin(x) in the above program:
Example 2: Calculating Sin() function of User’s Input in radians
The library of cmath is included in the header of the program just like the earlier example. In this example, sin() of the input variable ‘A’ from the user is calculated:
#include<cmath>
using namespace std;
int main()
{
double A;
cout << "Enter value of angle in radians : ";
cin >> A;
cout<<"sin() of " <<A <<"= " <<sin(A) <<"\n\n";
return 0;
}
Here first cmath library is included with necessary header files and then a double variable ‘A’ has been defined. Next the user is prompt to input the angle in radians and afterward the sine is calculated using sin(A) for the user’s input value in the above program:
Example 3: Calculating sin() function of User’s Input in degrees
This example code calculates the sine of the angle in degrees entered by the user:
The float variable ‘x’ has been defined for the user’s input of the angle in degrees. The program then converts the angle from degrees into radians using the ‘y’ variable and displays the value in the radians. The sin() function is then calculated by defining another float variable of ‘result’ for displaying the calculated sine value of input angle in degrees by the user.
Conclusion
The sin() function calculates sine of angles in radians for different double, float and long double variables. Three examples in this article showed three different ways in which sine of value can be calculated.