write a program in C++ to find the sum of cos series(two inputs are x and n and x is not in degrees)
Answers
Answer:
// CPP program to find the
// sum of cos(x) series
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double cosXSertiesSum(double x,
int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
{
sign = sign * -1;
fact = fact * (2 * i - 1) *
(2 * i);
pow = pow * x * x;
res = res + sign *
pow / fact;
}
return res;
}
// Driver Code
int main()
{
float x = 50;
int n = 5;
cout << cosXSertiesSum(x, 5);
return 0;
}
// CPP program to find the
// sum of cos(x) series
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double cosXSertiesSum(double x,
int n)
{
x = x * (PI / 180.0);
double res = 1;
double sign = 1, fact = 1,
pow = 1;
for (int i = 1; i < 5; i++)
{
sign = sign * -1;
fact = fact * (2 * i - 1) *
(2 * i);
pow = pow * x * x;
res = res + sign *
pow / fact;
}
return res;
}
// Driver Code
int main()
{
float x = 50;
int n = 5;
cout << cosXSertiesSum(x, 5);
return 0;