Answer to Question #57821 in Java | JSP | JSF for Nithya

Question #57821
Write a program that asks the user to input an integer, x, and calculates and prints cos(x) using the above equation series accurate to 10 terms. The final answer should be printed with 5 digits of accuracy after the decimal place.
1
Expert's answer
2016-02-16T03:04:23-0500
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = (double) scanner.nextInt();
double res = 0;
for (int i = 0; i < 10; i++) {
double factorial = 1;
for (int j = 1; j <= 2 * i; j++) {
factorial *= j;
}
res += (i % 2 == 0 ? 1.0 : -1.0) / factorial * Math.pow(x, 2 * i);
}

System.out.println(String.format("%.5f", res));
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS