In C language using ChIDE The function ex can be represented

In C language using ChIDE.

The function e^x can be represented in a Taylor series by: e^x = sigma_n=0^infinity x^n/n! Write a program in a script file that determines e^x by using the Taylor series representation. The program calculates e^x by adding terms of the series and stopping when the absolute value of the term that was added last is smaller than 0.0001. Use a loop, but limit the number of passes to 30. If in the 30th pass the value of the term that is added is not smaller than 0.0001, the program stops and displays a message that more than 30 terms are needed.

Solution

// C code

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

int factorial( int number )
{
int result = 0;

if ( number == 1 )
return 1;
else
        result = number * factorial( number - 1 );

return result;
}

float epowerx( int x, int n )
{
int i = 1;
float result = 1;

while ( true )
{
   float val = ( float ) pow( x, i ) / factorial( i );

   if(i > n)
   {
       printf(\"More than 30 terms needed\ \");
   }

   else if(val < 0.0001)
       break;

   else
           result = result + val;

++i;
}

return result;
}

int main()
{
int n = 30;
float x = 1.0f;
printf(\"e^x = %f\ \", epowerx(x,n));
return 0;
}

/*
output:

e^x = 2.718254

*/

In C language using ChIDE. The function e^x can be represented in a Taylor series by: e^x = sigma_n=0^infinity x^n/n! Write a program in a script file that dete
In C language using ChIDE. The function e^x can be represented in a Taylor series by: e^x = sigma_n=0^infinity x^n/n! Write a program in a script file that dete

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site