degree/radian to sin, cos and tan..

I have completed this coding, but its showing incorrect results.. can anyone help me to fix so that can get the correct output..

===============================================
#include <iostream>
#include <string>
using namespace std;
const double PI=3.14159265;

double rad (double angle);
double cosine (double angle);
double sine (double angle);
double tangent(double angle);

int main()
{
double angle,radian,degree,cos,sin,tan;
char res;

do
{

cout<<"***********TRIGNOMETRY PROGRAM**********\n";
cout<<"\n";
cout<<"\t -WELCOME TO THE PROGRAM-\n";
cout<<"\n";
cout<<"Please enter a angle value => ";
cin>>angle;
cout<<"\n";
cout<<"Is the angle in Degree or Radian?\n";
cout<<"\t Type D if it is in Degree\n";
cout<<"\t Type R if it is in Radian\n";
cout<<"Your response => ";
cin>>res;
cout<<"\n";

if ((res=='D' || res=='d'))
{

radian=rad(angle);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(7);
cout<<angle<<" "<<"Degree= \t"<<radian<<" "<<"Radian "<<endl;
cout<<"\n";

cout<<"\t RESULTS\n";
cout<<"========================================\n";
cout<<"\t x = "<< radian;
cout<<"\t Radian\n";
sin= radian*2;
cos= radian*3;
tan= radian*5;
cout<<"\t sin(x)= " <<sin<<endl;
cout<<"\t cos(x)= " <<cos<<endl;
cout<<"\t tan(x)= " <<tan<<endl;
cout<<"========================================\n";
cout<<"\n";
cout<<"Have a nice day! \n";
cout<<"\n";
}

else if ((res=='R' || res=='r'))
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(7);
cout<<"\t RESULTS\n";
cout<<"========================================\n";
cout<<"\t x = "<<angle<<"\t"<<" "<<"Radian"<<endl;
sin= angle*2;
cos= angle*3;
tan= angle*5;
cout<<"\t sin(x)= " <<sin<<endl;
cout<<"\t cos(x)= " <<cos<<endl;
cout<<"\t tan(x)= " <<tan<<endl;
cout<<"========================================\n";
cout<<"\n";
cout<<"Have a nice day! \n";
cout<<"\n";
}

else
{
cout<<"Ooops, invalid response, try again!\n";
cout<<"\t Type D if it is in Degree\n";
cout<<"\t Type R if it is in Radian\n";
cout<<"Your response => ";
cin>>res;

if ((res=='D' || res=='d'))
{

radian=rad(angle);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(7);
cout<<angle<<"Degree= \t"<<radian<<" "<<"Radian "<<endl;
cout<<"\n";

cout<<"\t RESULTS\n";
cout<<"========================================\n";
cout<<"\t x = "<< radian;
cout<<"\t Radian\n";
sin= radian*2;
cos= radian*3;
tan= radian*5;
cout<<"\t sin(x)= " <<sin<<endl;
cout<<"\t cos(x)= " <<cos<<endl;
cout<<"\t tan(x)= " <<tan<<endl;
cout<<"========================================\n";
cout<<"\n";
cout<<"Have a nice day! \n";
cout<<"\n";
}

else if ((res=='R' || res=='r'))
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(7);
cout<<"\t RESULTS\n";
cout<<"========================================\n";
cout<<"\t x = "<<angle<<"\t"<<" "<<"Radian"<<endl;
sin= angle*2;
cos= angle*3;
tan= angle*5;
cout<<"\t sin(x)= " <<sin<<endl;
cout<<"\t cos(x)= " <<cos<<endl;
cout<<"\t tan(x)= " <<tan<<endl;
cout<<"========================================\n";
cout<<"\n";
cout<<"Have a nice day! \n";
cout<<"\n";
}

}
cout<<"Do you want to continue?\n";
cout<<"\t Type Y to continue\n";
cout<<"\t Type any another key to stop\n";
cout<<"Your response => ";
cin>>res;
cout<<"\n";

}
while (res=='y' || res=='Y');
cout<<"Thank you, goodbye!!\n" ;

return 0;
}

double rad(double angle)
{
return(angle*PI)/180;
}

Last edited on
@haterz

First please always use code tags, edit your post, select all the code, press the <> button on the right.

Just wondering what you are trying achieve with this:

1
2
3
sin= radian*2;
cos= radian*3;
tan= radian*5;


You haven't defined these functions :

1
2
3
double cosine (double angle);
double sine (double angle);
double tangent(double angle);


Why aren't you using the math library functions?

You have a lot of repetition in your code.
You gotta define your functions after the main() block. Finding the sine and the cosine of an an angle without using the <cmath> library is much more complicated. cos x = 1 - x^2/2! + x^3/3! - x^4/4! + .....-x^n/n!. You will need two functions for the formula above one for the exponent and one for the factorial....
Topic archived. No new replies allowed.