Ads 468x60px

Saturday, March 10, 2012

Calculate Sin(X)


   1:  #include <iostream.h>
   2:  #include <math.h>
   3:   
   4:  int fact(int);
   5:   
   6:  void main()
   7:  {
   8:  float x,sinx=0,Result,b;
   9:  int p=1,i,counter=0,n=1;
  10:  cout<<"Enter Positive integer between\n";
  11:  cin>>x; // Read Positive Number
  12:  if(x==999)
  13:  cout<<"end program";
  14:  else if(x>0)
  15:  {for(i=0;i<100;i++)
  16:   
  17:   
  18:  {   b = pow(x,p)/fact(p);
  19:      Result = pow(x,p)/fact(p);
  20:      Result *=n;
  21:      sinx = sinx + Result;
  22:      p+=2;
  23:       counter++;   // To Calculate The Number Terms
  24:      n*=-1;
  25:      if(b<0.0001) break;  // To Stop When the value of term become <0.0001
  26:  }
  27:   
  28:  cout<<"The Sum sin(x) = "<<sinx<<endl;
  29:  cout<<"The Number Of Terms = "<<counter;
  30:  }
  31:  else
  32:  cout<<"Error(Enter Positive Number)";
  33:  }
  34:  int fact(int n)
  35:  {
  36:  if(n==1)
  37:  return 1;
  38:  else
  39:  return n*fact(n-1);
  40:  }

No comments:

Post a Comment