The equation to compute sinxusing infinite series is given b
The equation to compute sin(x)using infinite series is given below: sin(x) = sigma_n = 0^infinity T_n = sigma_n = 0^infinity (-1)^n times 2^n + 1/(2n + 1)! = x - x^3/3! + x^5/5!-... Note that the value of x is given in radians. Depending on the accuracy required, we can compute each term and keep adding it to the partial sum of infinite series until the next term is negligibly small value denoted by epsilon (eps), say less than 0.000001. Write a program to calculate the value of sin(x) using the above infinite series. The program prompts the user to enter the values of x and eps. It then calculates the value of sin(x) and prints it out on the screen. Test your program for the following values (assume eps = 0.000001): x = 0.785398, x = 6.083185, x = 4.112389, x = 7.225665
Solution
#include<iostream>
#include<cmath>
using namespace std;
long long int fact(long long int n){
if(n==1)
return 1;
else
return n*fact(n-1);
}
int main(){
double e=0.000001,sum=0.0,small=0.16,x;
cin>>x;
long long int n=1,i=0;
while(abs(small)>e){
if(i%2==0)
sum+=pow(x,n)/fact(n);
else sum-=pow(x,n)/fact(n);
i++;
small=pow(-1,i%2)*pow(x,n)/fact(n);
n=n+2;
}
cout<<sum;
}
