Answer to Question #273767 in C++ for sagun poudel

Question #273767

Write a program in C/C++ to calculate the value of “ ” by using the series expansion given below:

cosx=1-x2/2! + x^4/4! - x6/6!+.........

Note:

 Evaluate only upto first three terms.

 Also find the value of by using the inbuilt function.

 Compare the results i.e., the result produced by your program and that produced by inbuilt function. Based on comparison, determine error.




1
Expert's answer
2021-12-01T06:40:48-0500
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double Cos(double M, int n)
{
    M = M * (PI / 180.0);
 
    double output = 1;
    double cosine = 1, F = 1, pow = 1;
    for (int i = 1; i < 5; i++) {
        cosine = cosine * -1;
        F = F * (2 * i - 1) * (2 * i);
        pow = pow * M * M;
        output = output + cosine * pow / F;
    }
 
    return output;
}
 
int main()
{
    float M = 50;
    int n = 3;
    cout << Cos(M, 3);
    cout<<"\nPercentage is: "<< Cos(M, 3)*100;
    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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS