Check Answer.

#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j,n,fact,sign=-1;
 float x, p=1,sum=0;
 cout<<"Enter the value of x : ";
 cin>>x;
 cout<<"Enter the value of n : ";
 cin>>n;

 for(i=2;i<=n;i+=2)
 {
  fact=1;
  for(j=1;j<=i;j++)
  {
   p=p*x;
   fact=fact*j;
  }
  sum+=sign*p/fact;
  sign=-1*sign;
 }
 cout<<"cos "<<x<<"="<<1+sum;

 getch();
 return 0;
}
Output:
Enter the value of x : 1
Enter the value of n : 5
cos 1 = 0.541667

Post a Comment

Previous Post Next Post