import cs1.Keyboard;
public class wenjuan {
public static void main(String[] args) {
int count=1;
int n=Keyboard.readInt();
int total=1;
int halfcount=count/2;
double cos1=0;
while(count<=n){
if (count%2==0)
{
for (int i=1; i<count; i++)
{
total=total+total*i;
for (int j=1;j<halfcount; j++)
{
if (j%2==0)
cos1+= 1/total;
else
cos1+= (-1)/total;
System.out.println(cos1);
}
}
}
count++;
System.out.println(count);
}
}
}
The cosine function can be approximated as the summation of an infinite
series. For example,
cos(1)=1-1/(2!)+1/(4!)-1/(6!)+1/(8!)-.....
Complete the program below to use the first n terms of the above series to
approximate and print cos(1). Note that n!=1?2?3???n and 0!=1. You must use
at least one while loop. The only method from the Math class that you may
invoke is pow. All code should be written within method main.
Hint: Decompose the problem! Here are some (not all) of the tasks that the
program needs to perform:
? Accumulate terms in a sequence
? Calculate the factorial of a number
? Generate alternating signs (think about powers of ¨C1)