Page 1 of 1

Reputation:

# how to calculate sin(x)

Posted 02 October 2010 - 07:03 PM

Hey guy !!!
i don't know where is the probem in my code. i made a java code of 3 for-loops. the first one generates the numbers from 1, 3, 5, 7, .. the second one calculates the power, and the last one does the factoriel.

when i run the code, i do not get the right value for x.

``` import java.util.Scanner;
public class SinX
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);

System.out.print("Enter a number: ");
int x = kbd.nextInt();

double sum =0;
int s=1;
int p=1;
int fact =1;

for (int i=1; i<=x; i+=2)

{
for(int k=1; k<=i; k++)

{
p=p*x;

for (int j=1; j<=i; j++)
{
fact = fact*j;
sum = ((double)p/fact)*s;
s =- s;
}
}

}
System.out.print("The sum is: "+ sum);

}
}

```

This post has been edited by macosxnerd101: 02 October 2010 - 07:24 PM
Reason for edit:: Added code tags. Please use them in the future.

Is This A Good Question/Topic? 0

## Replies To: how to calculate sin(x)

### #2 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

## Re: how to calculate sin(x)

Posted 02 October 2010 - 07:07 PM

Please use the [code] tags when posting your code here at D.I.C.

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• Joined: 27-December 08

## Re: how to calculate sin(x)

Posted 02 October 2010 - 07:29 PM

Take a look at the Taylor Series Definition of Sine. It is the sum of xn/n!, alternating between negative and positive on each iteration. Note that n increases by 2 on each iteration.