Advertisement

Write a program to evaluate the function to 0.00001 accuracy cosx=1-x^2/2!+x^4/4!-x^6/6!...............................

#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(void)
{
  int i,j,n,factorial,sign;
  double a,sum=0,x,term;
  printf("Enter the value of x in degrees:=\n\t\t");
  scanf("%lf",&x);
  x=3.142*x/180.0;
  printf("Enter the no of terms:=  ");
  scanf("%d",&n);

  for(i=1;i<=n;++i)
   {
       for(factorial=1,j=1;j<=2*(i-1);++j)
       factorial*=j;
       sign=pow((-1),i+1);
      a=pow(x,2*(i-1))/(double)factorial;
      if(a<=.0001)
       break;
      term=sign*a;
      sum+=term;
  }

 printf("cos(%.4lf)=%lf",x,sum);

 getch();
 }

No comments:

Post a Comment

Please comment bellow. Share your knowledge