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: cos(x) program

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question cos(x) program

    I cannot figure out what I did wrong with this code to calculate cos(x), the function for the program is attached!

    package cosx;

    import java.util.Scanner;

    public class cosx {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter amount in degrees: ");
    double degrees = input.nextDouble();
    System.out.print("Enter the number of series: ");
    int n = input.nextInt();

    System.out.println(getCos(degrees, n));
    System.out.println(Math.cos(convertToRadians(degre es)));
    }

    public static double getCos(double degrees, int n) {
    double cos = 0;
    double numerator = 0;
    int denominator = 0;
    for (int i = 0; i <= n; i++, i++) {
    numerator = getNumerator(Math.pow(-1, i));
    denominator = getFactorial(2 * i);
    cos += numerator / denominator;
    }
    return cos;
    }

    public static double convertToRadians(double degrees) {
    return degrees * Math.PI / 180;
    }

    public static int getFactorial(int n) {
    int factorial = 1;
    for (int i = 1; i <= n; i++) {
    factorial *= i;
    }
    return factorial;
    }

    public static double getNumerator(double degrees, int n) {
    return convertToRadians(degrees) * Math.pow(-1, n);
    }

    }
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: cos(x) program

    what I did wrong with this code
    Please explain.

    Attachment image too small.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. cos & sin 2d movement
    By Johnharrisanglia in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2013, 04:23 PM
  2. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  3. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  4. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  5. main calls Math.cos and myCos twice.....
    By kimchi999 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 5th, 2010, 07:11 AM