C++ cos()

Last Updated On
Monday 18th Apr 2022

math cos

The C++ cos() function returns the cosine of an angle given in radian. It is defined in <cmath> header file.

Syntax

	cos(x)
	

Parameters

The cos() function takes a one argument in radians.

Return value

The cos() function returns the value in the range of [-1, 1].Return value Cosine of x radians.That value is either in double, float, or long double.

cos degrees

cos() prototype [As of C++ 11 standard]

	double cos(double x);
float cos(float x);
long double cos(long double x);
double cos(T x); // additional overloads for integral types
	

cosine pi

Example 1: C++ cos()

	#include <stdio.h>
#include <math.h>

#define PI 3.14159265

int main () {
  double param, result;
  param = 11.0;
  result = cos ( param * PI / 180.0 );
  printf ("The cosine of %f degrees is %f.\n", param, result );
  return 0;
}
	

Output

	The cosine of 11.000000 degrees is 0.981627.
	

cos function

Example 2 : With the Negative Value

	#include <stdio.h>
#include <math.h>

#define PI 3.14159265

int main () {
  double param, result;
  // let take negative value
  param = -22.0;
  result = cos ( param * PI / 180.0 );
  printf ("The cosine of %f degrees is %f.\n", param, result );
  return 0;
}
	

Output

	The cosine of -22.000000 degrees is 0.927184.
	

When the value is negative but it remains the same as cos(-x) = cos(x)

cosine c

Example 3 : C++ code with integral type

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

int main() {
  int x = 4;
  double result;
  // result is in double
  result = cos(x);
  cout << "cos(x) = " << result << endl;
  return 0;
}
	

Output

	cos(x) = -0.653644
	

References