/* compute cos(x) using the first 5 terms of the series. */ #include #include using namespace std; double approx(double); int fact(int); int main( ) { double x; cout << "Enter the value of x: "<> x; cout << "The value of cos(x) using C++ library function is: " << cos(x) << endl; cout << "The value of cos(x) using the first 5 terms of the series is: " << approx(x) << endl; return 0; } double approx(double x) //compute cos(x) using the first 5 terms of the series { double y; y = 1.0 - pow(x,2)/fact(2) + pow(x,4)/fact(4) - pow(x,6)/fact(6) + pow(x,8)/fact(8); return y; } int fact(int n) // compute n! { int nfact=1; while (n>1) {nfact = nfact * n; n--; } return nfact; }