C++ Program to find the sine of given radian value


Sine is a trigonometric parameter that deals with the right-angled triangle. The sine is the ratio of the opposite side to the hypotenuse. While calculating sine, we need the angle between the hypotenuse and the opposite side. Let the angle is 𝜃. The sin(𝜃) is like below −

$$\mathrm{sin(\theta)\:=\:\frac{opposite}{hypotenuse}}$$

In this article, we shall discuss the techniques to get the value of sin(𝜃) in C++ when the angle is given in the radian unit.

The sin() function

To compute the sin(𝜃) we need to use the sin() method from the cmath library. This function takes the angle in radian and directly returns the result. The syntax is simple, like below −

Syntax

#include < cmath >
sin( <angle in radian> )

Algorithm

  • Take angle x in radian as input
  • Use sin( x ) to calculate the sin (x𝑥)
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( float x ) { float answer; answer = sin( x ); return answer; } int main() { cout << "The value of sin( 2.5 ) is: " << solve( 2.5 ) << endl; cout << "The value of sin( 3.14159 ) is: " << solve( 3.14159 ) << endl; cout << "The value of sin with an angle of 30 degrees is: " << solve( 30 * 3.14159 / 180 ) << endl; cout << "The value of sin with an angle of 45 degrees is: " << solve( 45 * 3.14159 / 180 ) << endl; }

Output

The value of sin( 2.5 ) is: 0.598472
The value of sin( 3.14159 ) is: 2.53518e-06
The value of sin with an angle of 30 degrees is: 0.5
The value of sin with an angle of 45 degrees is: 0.707106

In this example, for two inputs, we have given the values in radians, and for the last two the angles are given in degrees but converted into radians with the following formula

$$\theta_{rad}\:=\:\theta_{deg}\:\times\:\frac{\pi}{180}$$

Conclusion

The sin$(\theta)$ function in C++ is used to get the sin$(\theta)$ value for the given 𝜃 in the radian unit. This function is a standard library function, but to use this, we need to include the cmath header file in our C++ code. Based on the versions of C++, the C90 version had return type as double, but in later versions, functions are overloaded for float, long double, and also use generic (template) in advanced usage for integral types. In this article, we have used this function with a few different parameters in radians or degrees but for degrees, the values are converted into radians with the above-mentioned formula.

Updated on: 17-Oct-2022

423 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements