% implement the Taylor series for the cosine function in two (conceptually

% identical) ways

x=input('x=');

n=input('n=');

sum=0;

for (j=0:2:2*n)

    factorial(j);

    sum=sum+(-1)^(j/2)*x^j/factorial(j);

end

fprintf('%12.8f\n',n,sum);



sum=1;

sign=1;

jfact=1;

xtoj=1;

for (j=2:2:2*n)

    sign=-sign;

    jfact=jfact*j*(j-1);

    xtoj=xtoj*x*x;

    sum=sum+sign*xtoj/jfact;

end

fprintf('%12.8f\n',n,sum);

    