C++ sin() Function

  • The sin() is the built-in function in C++
  • This function is defined in the <cmath> library – you have to include this in the header section.
  • The sin() function takes one argument i.e. a number which is angle and it returns the sine of angle x radians.
  • The return value is of double type i.e. sine of the given argument x (radians)

Syntax of sin() function

sin(x);

An example of using sin() function

In the first example, we simply provide a value to the sin() function and assign the return value to a double-type variable. Then we displayed the result as follows:

#include <iostream>

#include<cmath>

using namespace std;

int main()

{

  //angle in radians

  double ang=1.04;

  cout<<"Sine of 1.04 = "<<sin(ang) <<"\n\n";

  return 0;

}

Result:

Sine of 1.04 = 0.862404

Taking user input for sin()

As you run this C++ program, it asks you to enter the angle given in radians. Then sin() function is used to get the sine and we will display the result:

#include <iostream>

#include<cmath>

using namespace std;

int main()

{

  //angle in radians

    double ang;

    cout << "Enter angle (in radians) to get sine : ";

    cin >> ang;

    cout<<"Sine of " <<ang  <<"= " <<sin(ang) <<"\n\n";


  return 0;

}

Sample result:

CPP-sine-rad

Using negative value example

For this example, we assigned a negative value to the sin() function.

#include <iostream>

#include<cmath>

using namespace std;

int main()

{

  //negative angle

    double ang = -45;

  //angle to radians

  double rad=ang*3.14/180;

  //Using radian in sin() function

  cout<<"Sine of " <<rad <<" = " <<sin(rad);


  return 0;

}

Output:

Sine of -0.785 = -0.706825

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!