fork download
  1. /* Computes the Taylor Series for e^x for n terms */
  2. #include<stdio.h>
  3. float powerR(float x, int n){
  4. if (n == 0) return 1.0;
  5. return x * powerR(x,n-1);
  6. }
  7. int factR(int n){
  8. if (n == 1) return 1;;
  9. return n * factR(n-1);
  10. }
  11. float TaylorR(int x, int n){
  12. if (n == 0) return 1.0; // Since the series starts with 1
  13. return TaylorR(x,n-1) + (powerR(x,n)/factR(n));
  14. }
  15. float Taylor( int x, int n){
  16. int i;
  17. float sum = 0.0;
  18. printf("Talor Series with x=%d and n=%d: ",x,n);
  19. for(i=1;i<n;i++)
  20. sum = sum + (powerR(x,i)/factR(i));
  21. return sum = sum + 1; //Since series starts with 1
  22. }
  23. int main(){
  24. int x, n, i;
  25. printf("Give the values of x and n (number of terms):");
  26. scanf("%d",&x); scanf("%d",&n);
  27. for(i=1;i<=n;i++) printf("%f\n",Taylor(x, i));
  28. for(i=1;i<=n;i++) printf("%f\n",TaylorR(x, i-1));
  29. }
  30.  
  31.  
Success #stdin #stdout 0.01s 5392KB
stdin
4
4
stdout
Give the values of x and n (number of terms):Talor Series with x=4 and n=1: 1.000000
Talor Series with x=4 and n=2: 5.000000
Talor Series with x=4 and n=3: 13.000000
Talor Series with x=4 and n=4: 23.666668
1.000000
5.000000
13.000000
23.666668