Monday, September 19, 2016

A C++ Program to compute the cosine series ie., cos(x) = 1 - (x^2/2!) + (x^4/4!) - (x^6/6!) + .... (x^n/n!)

Tags

//C++ Program to compute the cosine series

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define PI 3.14159

int main()
{
   float x, temp, sum;
   int i , n;
   cout<<"\n Enter the number of cosine terms you want : ";
   cin>>n;
   cout<<"\n Enter the value of the variable x :";
   cin>>x;
   x = x * PI / 180;          //to convert x into radian
   temp += 1;
   sum = 1;
   for(i = 1; i <= n ; ++i)
   {
     temp = temp * pow((double)(-1),(double)(2*i - 1)) * x * x/(2*        i*(2*i - 1));
     sum += temp;
   }
   cout<<"\n cos(x) ="<<sum;
   getch();
   return 0;
}