124. PHP Program to Compute the Given Exponential Series 1 + x + x^2/2! + x^3/3! + x^4/4! + …… x^n/n!
How does this program work?
- This program is used to find the Sum of the given Exponential series 1 + x + x^2/2! + x^3/3! + x^4/4! + …… x^n/n! using PHP.
- The integer entered by the user is stored in variable x and n .
- By using the for loop condition we can easily calculate the Exponential series.
Here is the code
<html>
<head>
<title>Find the Sum of Exponential Series</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter x value"/> </td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter n value"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$x = $_POST['num1'];
$n = $_POST['num2'];
$x1 = $x;
$total = 1.0;
$precision = 3;
/* Loop to calculate the value of Exponential */
for($i=1;$i<=$n;$i++)
{
$total = $total + (pow($x, $i) / $i);
}
echo "X value : ".$x1."</br>";
echo "N value : ".$n."</br>";
echo "Sum of Exponential series is : " .number_format($total, $precision);
return 0;
}
?>
</body>
</html>