c sharp

# How to Work with the sin() Function in C++: Examples Included

The basic trigonometric functions can be calculated in C++ using the ‘cmath’ library. This article describes how to calculate sine of the given angle in radians along with various examples to use sin() function in C++. The input to sin() function is value in radians or degrees and the output of sin() function is a numeric value.

## 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

<strong>sin(x)</strong>

## 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 <iostream>

#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 <iostream>

#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:

#include <iostream>

#include<cmath>

using namespace std;

int main()

{

float x;

float y;

float result;

cout << "Enter angle in degrees : ";

cin >> x;

y = x*3.14159/180;

cout << "Calculated value in radians : "<< y <<"\n";

result = sin(y);

cout<<"sin() of " <<y <<"= " <<sin(y) <<"\n\n";

return 0;

}

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. 