124k views
4 votes
Please create C program, the task is to implement a function edoublepowerx that has an input parameter x of floating-point value (real numbers), and then return the approximation of e^2x as computed using the formula e^2x = 1 + 2x / 1! + 4x^2 / 2! + 8x^3 / 3! + ... + 1024x^10 / 10! . Use this function in a C main program that asks users for the x value, perform the calculation, and output the result. To assist the implementation, you may use the recursive function fact(n) and power(x,n).

User SeaSky
by
4.8k points

1 Answer

3 votes

Answer:

// here is code in C.

#include <stdio.h>

// function that calculate factorial of a number recursively

long int fact_num(int num)

{

if (num >= 1)

return num*fact_num(num-1);

else

return 1;

}

// function that calculate power of a number recursively

int num_pow(int b, int p)

{

if (p != 0)

return (b*num_pow(b, p-1));

else

return 1;

}

//edoublepowerx function

float edoublepowerx(int x){

int i;

float tot_sum = 1.0;

for(i = 0; i <= 10;i++ ){

tot_sum = tot_sum + (num_pow(2,i)*num_pow(x,i))/fact_num(i);

}

return tot_sum;

}

// driver function

void main()

{

// variables

float x,res;

printf("Please enter the value of x : ");

//read the value of x

scanf("%f",&x);

// call the function to calculate the value

res = edoublepowerx(x);

// print the result

printf("e^2x = %f",res);

}

Step-by-step explanation:

In the main function, first read the value of "x" from user.Then call the function edoublepowerx() with parameter "x".In this function, total of the equation is calculated by summing all the term one by one with the help of for loop. In the for loop,"(num_pow(2,i)*num_pow(x,i))/fact_num(i)" for each value of "i". here num_pow(), calculate the power of number recursively and fact_num() calculated the factorial of a numbers recursively.After the loop ends edoublepowerx() will return the value to the equation.

Output:

Please enter the value of x : 3

e^2x = 383.000000

User Teja Goud Kandula
by
4.8k points