C++ sin()

This function is defined in <cmath> header file.


[Mathematics] sin x = sin(x) [In C++ Programming]

sin() prototype (As of C++ 11 standard)

double sin(double x);
float sin(float x);
long double sin(long double x);
double sin (T x); // For integral type

sin() Parameters

The sin() function takes a single mandatory argument in radians.


sin() Return value

The sin() function returns the value in the range of [-1, 1]. The returned value is either in double, float, or long double.

Note: To learn more about float and double in C++, visit C++ float and double.


Example 1: How sin() works in C++?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  double x = 0.439203, result;
  
  result = sin(x);
  cout << "sin(x) = " << result << endl;
  
  double xDegrees = 90.0;
  
  // converting degrees to radians
  x = xDegrees*3.14159/180;
  result = sin(x);
  
  cout << "sin(x) = " << result << endl;

  return 0;
}

When you run the program, the output will be:

sin(x) = 0.425218
sin(x) = 1

Example 2: sin() function with integral type

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  int x = -1;
  double result;

  result = sin(x);
  cout << "sin(x) = " << result << endl;
  
  return 0;
}

When you run the program, the output will be:

sin(x) = -0.841471

Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community