# Program to Calculate e^x by Recursion

The value of Exponential function can be calculated using Taylor Series.

``` = 1 + x/1! + /2! + /3! + ......
```

To find its value using recursion, we will use static variables. For the power of x we will use p and for factorials we will use f as static variables.
The function shown below is used to increase the power of x.

`p = p*x `

The function below is used to find factorials.

`f = f*n`

The function below is used to calculate the summation of the series.

`r+p/f`

where r is the recursive call to the function.

Below is the implementation of the above idea.

## C++

 `// C++ implementation of the approach ` `#include ` ` `  `// Recursive Function with static ` `// variables p and f ` `double` `e(``int` `x, ``int` `n) ` `{ ` `    ``static` `double` `p = 1, f = 1; ` `    ``double` `r; ` ` `  `    ``// Termination condition ` `    ``if` `(n == 0) ` `        ``return` `1; ` ` `  `    ``// Recursive call ` `    ``r = e(x, n - 1); ` ` `  `    ``// Update the power of x ` `    ``p = p * x; ` ` `  `    ``// Factorial ` `    ``f = f * n; ` ` `  `    ``return` `(r + p / f); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `x = 4, n = 15; ` `    ``printf``(``"%lf \n"``, e(x, n)); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.text.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Recursive Function with static ` `// variables p and f ` `static` `double` `p = ``1``, f = ``1``; ` `static` `double` `e(``int` `x, ``int` `n) ` `{ ` `    ``double` `r; ` ` `  `    ``// Termination condition ` `    ``if` `(n == ``0``) ` `        ``return` `1``; ` ` `  `    ``// Recursive call ` `    ``r = e(x, n - ``1``); ` ` `  `    ``// Update the power of x ` `    ``p = p * x; ` ` `  `    ``// Factorial ` `    ``f = f * n; ` ` `  `    ``return` `(r + p / f); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `x = ``4``, n = ``15``; ` `    ``DecimalFormat df = ``new` `DecimalFormat(``"0.######"``); ` `    ``System.out.println(df.format(e(x, n))); ` ` `  `} ` `} ` ` `  `// This code is contributed by mits `

## Python3

 `# Python implementation of the approach ` ` `  `# Recursive Function  ` `# global variables p and f ` `p ``=` `1.0` `f ``=` `1.0` ` `  `def` `e(x, n) : ` ` `  `    ``global` `p, f ` `     `  `    ``# Termination condition ` `    ``if` `(n ``=``=` `0``) : ` `        ``return` `1` `     `  `    ``# Recursive call ` `    ``r ``=` `e(x, n ``-` `1``) ` `     `  `    ``# Update the power of x ` `    ``p ``=` `p ``*` `x ` `     `  `    ``# Factorial ` `    ``f ``=` `f ``*` `n ` `     `  `    ``return` `(r ``+` `p ``/` `f) ` ` `  `# Driver code ` ` `  `x ``=` `4` `n ``=` `15` `print``(e(x, n)) ` ` `  `# This contributed by ihritik `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Recursive Function with static ` `// variables p and f ` `static` `double` `p = 1, f = 1; ` `static` `double` `e(``int` `x, ``int` `n) ` `{ ` `    ``double` `r; ` ` `  `    ``// Termination condition ` `    ``if` `(n == 0) ` `        ``return` `1; ` ` `  `    ``// Recursive call ` `    ``r = e(x, n - 1); ` ` `  `    ``// Update the power of x ` `    ``p = p * x; ` ` `  `    ``// Factorial ` `    ``f = f * n; ` ` `  `    ``return` `(r + p / f); ` `} ` ` `  `// Driver code ` `static` `void` `Main() ` `{ ` `    ``int` `x = 4, n = 15; ` `    ``Console.WriteLine(Math.Round(e(x, n),6)); ` ` `  `} ` `} ` ` `  `// This code is contributed by mits `

Output:

```54.597883
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Mithun Kumar, ihritik