fork download
  1. #include <stdio.h>
  2.  
  3. float power(float,int);
  4. float ser(float,int);
  5. int fact(int);
  6.  
  7. int main()
  8. {
  9. float x,res;
  10. int n;
  11.  
  12. printf("Enter no of steps and x");
  13. scanf("%d %f",&n,&x);
  14. res = ser(x,n);
  15. printf("The result is %f\n",res);
  16. }
  17.  
  18. float ser(float x,int n)
  19. {
  20. float pw,fc,res=0;
  21. int i,j;
  22. for(i=1,j=1;i<=n;i++,j=j+2)
  23. {
  24. pw = power(x,j);
  25. fc = fact(j);
  26. if(i%2!=0)
  27. res = res + pw/fc;
  28. else
  29. res = res - pw/fc;
  30. }
  31. return res;
  32. }
  33.  
  34. float power(float b,int p)
  35. {
  36. int i;
  37. float res=1;
  38. for(i=1;i<=p;i++)
  39. res=res*b;
  40. return res;
  41. }
  42.  
  43. int fact(int n)
  44. {
  45. int i, res;
  46. for (res=1,i=1 ; i<=n ;i++)
  47. res = res * i;
  48. return res;
  49. }
  50.  
Success #stdin #stdout 0s 9424KB
stdin
5
2
stdout
Enter no of steps and xThe result is 0.909347