Answer to Question #162927 in C++ for prajakta

Question #162927

Write a program that takes as input two numbers n (integer) and x (double), and prints arctan(x) evaluated by taking n terms of the Taylor series (using x0 =0):

arctan(x) = x - x 3 /3 + x 5 /5 - x 7 /7 …



1
Expert's answer
2021-02-11T16:58:12-0500
#include <iostream>
#include <cmath>
using namespace std;
int main () {
    int n, p = 1;
    double x, S = 0;
    cout << "n = "; cin >> n;
    cout << "x = "; cin >> x;
    for (int i = 1; i < n; i++) {
        if (i % 2 == 1) {
            S += p * pow(x, i) / i;
            p *= (-1);
        }
    }
    cout << "arctan(" << x << ") = " << S;
    return 0;
}

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