how to calculate sin(x)

sin(x)=x-(x^3)/3!+(x^5)/5!-(x^7)/7!+.....

Page 1 of 1

2 Replies - 9789 Views - Last Post: 02 October 2010 - 07:29 PM Rate Topic: -----

#1 Guest_Joe*


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  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,592
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1