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: Sine function, can't get factorial to work

1. ## Sine function, can't get factorial to work

I've been having issues with trying to get this program to work. The program's purpose is to compute the sine function without using the Math.sin(). At first I had trouble with the for loop but I finally understood how it worked, so it's not much of an issue anymore. My next problem is trying to get this factorial to work. I've look at tutorials and I think I'm doing the right thing but when I compile I get several errors (stated in code).

Original Code

```/* This program is suppose to compute the Sine function without using Math.sin */

import java.io.*;
public class x
{
public static void main(String args[]) throws IOException
{

int MaxE;
Sine = 0.0;

for (int a = 1; a <= 11; a++)
{
}

System.out.println("The value of Sine is " + Sine);

}

}```

Errors

```x.java:20: cannot find symbol
symbol  : method factorial(int)
location: class x
^
1 error```

I think I'm on the right track but I'm not sure how to troubleshoot these errors yet since I'm still pretty new. Any help is appreciated!

2. ## Re: Sine function, can't get factorial to work

Hello Actinistia.

The issue is that factorial(2 * a + 1) is seen as a method and obviousally there isn't this method within your code.
factorial isn't even a variable within your code so what are you attempting to do here please?

3. ## Re: Sine function, can't get factorial to work

Originally Posted by JavaPF
Hello Actinistia.

The issue is that factorial(2 * a + 1) is seen as a method and obviousally there isn't this method within your code.
factorial isn't even a variable within your code so what are you attempting to do here please?
Well, I was looking at some code from a different forum, although I wasn't able to get it to work either, and it seemed to make some sense. I tried to incorporate it within my own code but wasn't able to do it successfully. Here's the code I'm referring to:

```public double sin(int n){
double sum = 0.0;
for(int i = 1; i <= n; i++){
//if i is even, -1 becomes 1
//else, -1 stays negative
sum += Math.pow(-1,i)/factorial(2*i+1);
}
return sum;
}

public double factorial(int i){
if(i <= 1) return 1;
double product = 1;
for(int j = i; j > 1; j--) product *= j;
return product;
}```

It's probably due to my own inexperience since I've only been working with java for a few weeks or so. I did have the
```public double factorial(int i){
if(i <= 1) return 1;
double product = 1;
for(int j = i; j > 1; j--) product *= j;
return product;
}```

in there before but I was getting errors so I just decided to delete it since I didn't know what to do with it.

4. ## Re: Sine function, can't get factorial to work

just this
import javax.swing.*;

class fak {

public static void main(String[]args) {

int hasil=1;

int f=Integer.parseInt(JOptionPane.showInputDialog("Ma sukkan Angka : "));

for(int i=1;i<=f;i++)

{

hasil=hasil*i;

if(f !=i)

System.out.print(i+"x");

else

System.out.print(i+" = ");

}