Results 1 to 6 of 6

Thread: how to do a loop in a calculation

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Posts
    13

    Unhappy how to do a loop in a calculation

    hi i need to do a loop for my following code to increase my number of sides by one till the sin is less than pi and pi is less than tan or sin and tan are both equal to pi. do i start my loop from the int sides = 4 by using the ++ sin <pi <tan .
    could you give me a hint in the right direction as i want to learn

    import java.math.*;
    import java.io.*;
    class ged1loop
    {
    public static void main(String [] args)throws IOException
    {
    int sides = 4;
    System.out.println("the angle of "+ sides+" sides is "+(180/sides) );
    double n = (180/ sides);
    System.out.println("Sine is: " +Math.sin (Math.toRadians(n)));
    System.out.println("Tangent is: " +Math.tan (Math.toRadians(n)));
    System.out.println("Sine times by sides : " +Math.sin (Math.toRadians(n))*sides);
    System.out.println("Tangent times by sides is: " +Math.tan(Math.toRadians (n))*sides);
    }
    }

  2. #2
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Search in your book about : IF and ELSE statement

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Posts
    13
    i have read my book from back to front, on loops it is easy to do a loop to increase the number of sides. i have done that. But the book don't mention how i can do a loop so that my adding number of sides will stop when the answer for sin is less than PI and also PI is less then TAN answer. eg when i run a loop say to increase the numbers sides up to 50. i would want it to stop at sides 48 as this would be right. sin< PI < tan
    sides 49 sin <PI but the tan is not
    the sin for 48 sides is 3.13950 ect which is less than PI
    the tan for 48 sides is 3.14608 ect which is greater than PI

    sides 49 sin is less than PI, but the Tan is less as well, which is no good. my answer would have to finnish with 48 sides

    the book has no examples for what i need or how to write it.

    i don,t mind learning myself, maybe i picked a hard one to figure out for a beginner. But if any one can recommend a book that will explain this. i will go and get it.

    thanks for your time, as i'm not pushing for the answer that would be lazy. just for help were i can read up on how if at all it is possible.

  4. #4
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    I'm guessing that you need something like this. Although I'm not sure how you got the values for sin and tan

    for (sides = 4; (Math.sin(Math.toRadians (sides)) >= Math.PI) || (Math.tan(Math.toRadians(sides)) <= Math.PI); sides++)
    {
    ...
    }

    If you want it to stop before 50 then just add && sides <= 50

    In degrees sin 48 = 0.743144825
    In radians sin = -0.768254661
    in radians sin(180/48) = -0.571561318

    So I'm not too sure how you're calculating your numbers, but you should be able to replace your method in the for loop
    || = or
    && = and

    Hope that helps

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2002
    Posts
    13
    thanks a million, i was about to lash the computer throu the windon. the sum i was working on was Archimedes Method. you are right over sin and tan its just that i had to multiply sin and tan again by number of sides. Good job Mr Arcimedes is dead as i would have killed him i was never good at maths.
    thanks again, no longer will i wake up in the middle of the night trying codes that was sending me mad!

  6. #6
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Ahh, makes more sense now. Glad to be of some help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width