sin and cos in cpp code example

Example 1: sine function in cpp

/* sin example */
#include <stdio.h>      /* printf */
#include <math.h>       /* sin */

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 30.0;
  result = sin (param*PI/180);
  printf ("The sine of %f degrees is %f.\n", param, result );
  return 0;
}

Example 2: how to calculate trigonometric values in c++

*
 * C++ Program to Illustrate Trigonometric functions
 */
#include <iostream>
#include <iomanip>
#include <cmath>
const double PI = 3.14159265;
 
int main()
{
    double degrees, radians;
 
    std::cout << "Enter angle in degrees ";
    std::cin >> degrees;
    // Trigonometric functions accept angles in radians
    radians = degrees * PI / 180.0;
    std::cout << "Value of trigonometric ratios " << std::endl;
    std::cout << "cos(radians) = " << std::setprecision(3)
              << cos(radians) << std::endl;
    std::cout << "sin(radians) = " << std::setprecision(3)
              << sin(radians) << std::endl;
    std::cout << "tan(radians) = " << std::setprecision(3)
              << tan(radians) << std::endl;
    return 0;
}

Tags:

Cpp Example