C++ Program to Find Trigonometric Ratios

This program is a demonstration of the use of C++ math header. Given 3 sides or angle values of a triangle, this program computes all 6 trigonometric ratios and print the result to the console.

The trigonometric ratios are calculated using builtin function from math.h header file.

Learn the basics of C programming before you try this example.

Problem Definition

The program gives two option to the user during run time. Depending on the available input values the user selects following.

  • Find the 6 trigonometric values for a given angle.
  • Find the 6 trigonometric values for given 3 sides.

If the user chooses the first option, then he or she must input the angle measure for which they wish to find the trigonometric ratio values.

The program than uses builtin trignometric functions from math.h header file such sin(), cosine() and tan() functions to computes the output.

Secondly, if the user choice is finding trig rations using the length of sides of the triangle, then they must enter the values of sides.

First, enter the length of two shorter sides – AB and BC – where AB is opposite side and BC is the adjacent side of a triangle.

Then enter the length of the longer side (hypotenuse), but the value is not accepted if it is less than AB and BC because a hypotenuse is the longest side in a triangle.

The program computes the trigonometric ratios in the following manner.

sin \theta = (opposite side/ hypotenuse) = AB/AC cos \theta = (adjacent side/ hypotenuse) = BC/AC tan \theta = (opposite side/ adjacent side) = AB/AC cosec \theta = (1/sin \theta) sec \theta = (1/cos \theta) cot \theta = (cos \theta / sin \theta)

Program Source Code

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
float sine,cosine,tangent,cotangent,cosecant,secant;
float rad,degree;
rad = degree = 0.0;
int choice;

//sine=cosine=tangent=cotangent=cosecant=secant = 0.0;

cout <<"1. Find trig ratios using radian measure:" << endl;
cout <<"2. Find trig ratios using length of sides:" << endl;

cout <<"Enter your choice:";
cin >> choice;

if(choice == 1)
{



cout << "Enter the angle in degrees:";
cin >> degree;

rad = (degree/180) * 3.14;

sine = sin(rad);
cosine = cos(rad);
tangent = tan(rad);
secant = 1/cosine;
cosecant = 1/sine;
cotangent = cosine/sine;



}
else if(choice == 2)
{
float opposite,adjacent,hypotenuse;

cout << "Enter the length of each side:" << endl; 
cout << "Length of opposite side(AB)?:" << endl;
cin >> opposite;

cout << "Length of adjacent side(BC)?:" << endl;
cin >> adjacent;

cout << "Length of hypotenuse (AC)?:" << endl;
cin >> hypotenuse;

sine = opposite/hypotenuse;
cosine = adjacent/hypotenuse;
tangent = opposite/adjacent;
secant = 1/cosine;
cosecant = 1/sine;
cotangent = cosine/sine;


}
else
{

cout << "Incorrect ! Choose correct option:" << endl;

} 

//checking valid values for sin,cos,tan,cosec,sec,cot before printing



if(sine >= -1.0 || sine <= 1.0)
{

cout << "Sin" << " " << degree << "=" << sine << endl;
} 
else
{
cout << "Sin" << "=" << "Undefined" << endl;
}

if(cosine >= -1.0 || cosine <= 1.0)
{

cout << "Cos" << " " << degree << "=" << cosine << endl;
}
else 
{
cout << "Cos" << "=" << "Undefined" << endl;
}



if(degree != 90)
{ 
cout << "Tan" << " " << degree << "=" << tangent << endl;
} 
else
{

cout << "Tan" << "=" << "Undefined" << endl;
} 

if(degree != 90)
{
cout << "Sec" << " " << degree << "=" << secant << endl;
} 
else
{
cout << "Sec" << "=" << "Undefined" << endl;

} 

if(degree != 0 && cosecant <= -1.0 || cosecant >= 1.0)
{
cout << "Cosecant" << " " << degree << "=" << cosecant << endl; 
}
else
{

cout << "Cosecant" << "=" << "Undefined" << endl;
}

if(degree != 0)
{
cout << "Cotangent" << " "<< degree << "=" << cotangent << endl;
}
else
{
cout << "Cotangent" << " " << "=" << "Undefined" << endl;
}

system("PAUSE");

return EXIT_SUCCESS;
}

Output

Output 1 - User Selects Option 1
Output 1 – User Selects Option 1
Output 2 - User select option 2
Output 2 – User select option 2

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag