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

1. ## 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));
}

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) {
}

}

2. ## Re: cos(x) program

what I did wrong with this code