C++ please! Write a recursive function to compute the sum of the Taylor series.
use double taylor(int a, int n)
Here is your code -
#include <stdio.h>
// Recursive Function with static
// variables p and f
double taylor(int a, int n)
{
static double taylor p = 1, f = 1;
double r;
if (n == 0)
return 1;
r = taylor(a, n - 1);
p = p * a;
// Factorial
f = f * n;
return (r + p / f);
}
// Driver code
int main()
{
int a = 4, n = 5;
printf("%lf \n", taylor(a, n));
return 0;
}
-----------------------------------------------------------
If you feel your purpose is not solved, go with this code. Although both are 100% working code. PLEASE DO GIVE A 'LIKE' ON MY POST. THANKS! DO COMMENT DOWN, IF YOU NEED ANY EXPLANATION OR ANY HELP REGARDING THIS QUESTION!
#include<stdio.h>
#include<math.h>
int main()
{
int x,i;
int fact = 1,n;
float sum=0;
cout<<"Enter the value of x in the series :";
cin>>x;
cout<<"Enter the number of terms in the series :";
cin>>n;
for(i=1;i<n;i++)
{
fact = fact*i;
sum = sum + (pow(x,i)/fact) ;
}
sum= sum +1; //Since series starts with 1
cout<<"The sum of the taylor series is
:"<<sum;
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.