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

# Thread: calculate sin(x)

1. ## calculate sin(x)

How can I transform this code to calculate the sin(x) in the method?

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(degrees, i);
denominator = getFactorial(2 * i + 1);
cos += numerator / denominator;

2. ## Re: calculate sin(x)

Can you use the Math class's method?

Otherwise what is the algorithm for the computation? Where are you having problems coding it?

Please edit your post and wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.

3. ## Re: calculate sin(x)

```package cosx;

import java.util.Scanner;

public class cosx2 {

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

System.out.print("Enter the degree: ");
double degree = input.nextDouble();

}

public static double getRadian(double x) {
return x = (Math.PI/180)*x;
}

private static void calculateCos(Scanner std, double rad, double deg) {
System.out.print("How many terms do you want to calculate the cos: ");
int terms = std.nextInt();
double top;
double bottom;
double sum=0;

for(int i = 0; i<= terms; ++i){
top = Math.pow(-1, i);
bottom =1;
int repeat = 2*i+1;
for(int j = 1; j <= repeat; ++j){
bottom *= j;
}
if(i%2 == 0)
sum += (top/bottom);
else
sum -= (top/bottom);
}
System.out.printf("The sin (%.1f", deg);
System.out.printf(") is %.6f", sum);
}
}```

I cannot figure out how to input the function to calculate the cosine?

5. ## Re: calculate sin(x)

how to input the function to calculate the cosine?
What do you mean by "input the function"?
Are you trying to type code for a method into an editor?
Are you trying to pass data as input to a method?

The attachment is useless. Too small.

6. ## Re: calculate sin(x)

If you click the image it will expand....
```import java.util.Scanner;

public class cosx2 {

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

System.out.print("Enter the degree please: ");
double degree = input.nextDouble();

}

public static double getRadian(double x) {
return x = (Math.PI/180)*x;
}

private static void calculateCos(Scanner std, double rad, double deg) {
System.out.print("How many terms do you want to calculate the cos: ");
int terms = std.nextInt();
double num;
double den;
double sum=0;

for(int i = 0; i<= terms; ++i){
num = Math.pow(-1, i);
den = factorial(2*i);
int repeat = 2*i+1;
for(int j = 1; j <= repeat; ++j){
den *= j;
}
if(i%2 == 0)
sum += (num/den);
else
sum -= (num/den);
}
System.out.printf("The cos (%.1f", deg);
System.out.printf(") is %.6f", sum);
System.out.println("The calcuated value of cos was " +sum+
" Howver the real value of cose was " + Math.cos(deg));

}
}```
Screen Shot 2013-11-06 at 11.11.44 AM.jpg

7. ## Re: calculate sin(x)

How are you debugging the code? I use println() statements to print out intermediate results to validate the code. Does the code compute each term in the series correctly? Print out the first few and manually verify that they are correct.

The code should include comments showing the series that is being computed.

It is impossible to copy the series shown in an image to be able to add it as comments to the code.