public class PolarCoordinates {

    public static void main(String[] args){

        int r=10;
        double x=0,y=0,theta=30;
     x=r*Math.cos(theta);
    y=r*Math.sin(theta);

        System.out.println("The value of x is :" +x);
        System.out.println("The value of y is :" +y);



}
}

help please?
it gives me wrong answer.
10cos(30) should equal to 8.7, but it gives me 11.7 ans.
10sin(30) should equal to 5, and it gives me 2.63 ans.
whats wrong with my code?

Dani AI

Generated

As noted, Java’s trig functions take radians, which is why ’s degree value produced unexpected results. correctly pointed toward converting the angle — the clearest, least error‑prone way in Java is to use Math.toRadians(...) before calling Math.cos/Math.sin.

int r = 10;
double degrees = 30.0;
double rad = Math.toRadians(degrees);
double x = r * Math.cos(rad);
double y = r * Math.sin(rad);
System.out.printf("x = %.3f, y = %.3f%n", x, y);

Quick practical tips not covered above: when using these coordinates for drawing in a GUI remember screen Y usually grows downward — invert the sine term or subtract from the canvas origin if you need a traditional Cartesian orientation. Also watch out for integer math when converting manually (e.g., 1/180 as integers yields 0); prefer Math.toRadians or do the conversion with doubles. Verify behavior with known angles (0°, 90°, 180°, 270°) to make sure your orientation and sign are what you expect.

Finally, keep angles as doubles, format output for readability, and, if you compute many points, compute the radian once and reuse it rather than converting inside loops.

Recommended Answers

All 7 Replies

sin. cos etc use angles in radians, your data (30) looks like degrees.

30° equals pi/6 rad

oh yes, its a degrees.
what's wrong with my formula?

To convert yout theta value from degrees to radians use: theta = (theta * 3.141592)/360;

what's wrong with my formula?

sin. cos etc use angles in radians (Google it), you have supplied an angle in degrees

Pi radians is 180 degrees, so ddanbe' formula should be theta = theta * Math.PI / 180;
(sorry ddanbe)

Oops!!! Thanks for correcting me JamesCherrill, 2*pi rad = 360° and not pi rad = 360° of course!

ohh, ok! thanks! i got it now :)

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.