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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Calculating Sin(x)

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculating Sin(x)

    I'm trying to calculate sin(x) without using Math.sin(x). The formula for sin(x) is: x - x^3/3! + x^5/5! ...
    I can't seem to get the coding for the alternating +/- right. Here's my program:

    import java.util.Scanner;
    import java.lang.Math;
    class Sin
    {
    public static void main(String[] args)
    {
    Scanner kb = new Scanner(System.in);
    int n, c, fact = 1, count = 1;
    double x, sum = 0, sum_sin = 0, result;

    System.out.println("Enter positive odd integer");
    n = kb.nextInt();

    System.out.println("Enter x");
    x = kb.nextDouble();

    for ( c = 1 ; c <= n ; c++ )
    fact = fact*c;

    while (count <= n)
    {
    result = Math.pow(x,count);
    sum = result/fact;
    count++;
    if (count % 2==0)
    {
    x = -x;
    count++;
    }


    sum_sin = sum_sin + sum;
    }


    System.out.println(sum_sin);
    System.out.println(Math.sin(x));
    }
    }





    It would be greatly appreciated if somebody could help me.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Calculating Sin(x)

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

Similar Threads

  1. Pi calculating help.
    By Unitize in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 6th, 2014, 11:40 AM
  2. calculate sin(x)
    By anon314 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 6th, 2013, 12:48 PM
  3. cos & sin 2d movement
    By Johnharrisanglia in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2013, 04:23 PM
  4. NEED SIN OF ANGLE
    By CLAYAROBINSON in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: June 2nd, 2011, 10:14 PM

Tags for this Thread