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.
#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;
}
Comments
Leave a comment