C++ sin() function [3 Examples]

The sin function in C++

  • sin() is the built-in function in C++
  • This function is defined in the <cmath> library – you have to include this in the header in order to use it.
  • 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 an 1.04 = "<<sin(ang) <<"\n\n";




  return 0;

}

Result:

CPP-sine

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:

CPP-sine-negative

 

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!