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.

New Topic/Question
Reply
MultiQuote









|