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

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

1. ## 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
{

String s;
int MaxE, i;

s = s.toLowerCase();
s = s.trim();

if (s.equals("d"))
{
System.out.println("Enter a value (Degrees): ");
}

else if (s.equals("r"))
{
}

System.out.println("Enter the desired maximum exponent: ");
****************************************************************
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
{
System.out.println("Enter the number");
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.  Reply With Quote

3. ## 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.  Reply With Quote

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

Actinistia (March 16th, 2011)

5. ## Re: Using for loop to calculate sine function (factorial issue) Originally Posted by copeg 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.  Reply With Quote