hi. 2 all
anyone can tell what is the wrong with this code? the result of this program is :
cos(60)=-0.952413
sin(60)=-0.304811

#include <iostream.h>
#include <math.h>
main()
{
 float x,y,z;
y=cos(60);
z=sin(60);
cout<<"cos(60)="<<y<<endl;
cout<<"sin(60)="<<z<<endl;
}

cheers

I don't see a problem there. I've been using Borland C++ for my home assignments, and your code compiled on it without errors. Then again, it is an outdated compiler, and you are probably using a different one.

Maybe you want to put getch() in the end, so that window does not close, when program is finished, and you can view the results?

The math.h functions take an angle in radians. Use the conversion pi radians is to 180 degrees to convert your angles before sending them in.

commented: And the one that solves this mystery +5

math.h trigonometric functions take parameters in radians. To convert radians to degrees :
cos (60 degrees) is equivalent to

cos (PI*60/180)

PI - predefined constant in math.h

PI - predefined constant in math.h

I think it's M_PI (although it may be redefined elsewhere).

I think it's M_PI (although it may be redefined elsewhere).

Yeah, sorry. The constant in cmath is M_PI.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.