Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using for loop to calculate sine function (factorial issue)

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Using for loop to calculate sine function (factorial issue)

    Hi again, I was working on another homework assignment and I got stuck trying to figure out why my program wasn't computing the right answer. The assignment was to basically write a program that computes the Sine function (as close as you possibly can) without using Math.sin(). I believe that my for loop in the original code is not doing the factorial for the right variable. I'm pretty sure the equation is right as well. I figured out the series needed to compute sin:

    Sine = (-1.0^n)(x^(2n+1)) / (2n+1)!

    Here's my original code:
    /* This program is suppose to compute the Sine function without using Math.sin */
     
    import java.io.*;
    public class HW5
    {
    public static void main(String args[]) throws IOException
    {
     
    BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
     
    String s;
    double Rad, Sine;
    int MaxE, i;
    Rad = 0.0;
     
     
    System.out.println("Degrees (d)? or Radians (r)? Please enter (d) or (r): ");
    s = keybd.readLine();
    s = s.toLowerCase();
    s = s.trim();
     
    if (s.equals("d"))
    {
    System.out.println("Enter a value (Degrees): ");
    Rad = Double.parseDouble(keybd.readLine());
    Rad = (Math.PI * Rad) / 180;
    }
     
    else if (s.equals("r"))
    {
    System.out.println("Enter a value (Radians): ");
    Rad = Double.parseDouble(keybd.readLine());
    }
     
    System.out.println("Enter the desired maximum exponent: ");
    MaxE = Integer.parseInt(keybd.readLine());
    ****************************************************************
    i = (2 * MaxE) + 1;                  //This is where I think I messed and confused myself
     
    for (int a = 0; a <= MaxE; a++)
    {
    i = a * i;
    }
     
    Sine = ((Math.pow(-1.0,MaxE))*(Math.pow(Rad, i)) / i!);  // I put the ! to indicate where I wanted the factorial to go
     
     
    System.out.println("The value of Sine is " + Sine);
     
    }
     
    }

    My first problem was trying to figure out how to compute a factorial. I looked through the tutorials and found some code for recursive factorials but I couldn't get it to work. I ended up finding a code that I got to work but the problem is is that I'm not sure how I could incorporate it into my original code.

    Factorial program:

    import java.io.*;
    public class s
    {
    public static void main(String args[])
    {
    try
    {
    BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number");
    int a = Integer.parseInt(object.readLine());
    int fact = 1;
    System.out.println("Factorial of " + a + ":");
    for (int i = 1; i <= a; i++)
    {
    fact = fact * i;
    }
    System.out.println(fact);
    }
    catch (Exception e) {}
    }
    }

    I apologize for this being so long but I'm not sure if my logic is right or not. Also, does java have a built in factorial method or did I have to make a loop for it? Any help is appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using for loop to calculate sine function (factorial issue)

    I ended up finding a code that I got to work but the problem is is that I'm not sure how I could incorporate it into my original code.
    If you don't know, you first should figure out how the code you found to calculate the factorial works. How does it calculate factorial? Knowing this will put you in a much better position to be able to reproduce its functionality in your code. What variable(s) are necessary, and what variable(s) does it produce as a result? Break the problem down, and perhaps to help you do so just isolate this functionality by writing a method that computes the factorial of a parameter and returns the result - this will let you focus primarily on this portion of the code independent of the rest.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Actinistia (March 16th, 2011)

  4. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Using for loop to calculate sine function (factorial issue)

    Quote Originally Posted by copeg View Post
    If you don't know, you first should figure out how the code you found to calculate the factorial works. How does it calculate factorial? Knowing this will put you in a much better position to be able to reproduce its functionality in your code. What variable(s) are necessary, and what variable(s) does it produce as a result? Break the problem down, and perhaps to help you do so just isolate this functionality by writing a method that computes the factorial of a parameter and returns the result - this will let you focus primarily on this portion of the code independent of the rest.
    Thanks for the suggestion. I was just about to go through that, in fact. I felt that just sitting around and waiting isn't going to help so I'm going through all my code and writing down what each part does and what I intended it to do. I should of down this first instead of asking for help right away. Hopefully this will be solved soon. Thanks again.

Similar Threads

  1. Unending Loop Issue? Can't find it.
    By computerguy38 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 4th, 2011, 08:22 PM
  2. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 05:10 PM
  3. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:13 PM
  4. Opposites of Sine Cosine and Tangent
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 28th, 2011, 01:59 PM
  5. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 12:09 PM