127. PHP Program to Compute the Given Series 1 - x^2/(2^2*1!) + x^4/(2^4*2!) – x^6/(2^6*3!)
How does this program work?
- This program is used to find the Sum of the given series 1 - x^2/(2^2*1!) + x^4/(2^4*2!) – x^6/(2^6*3!) 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 sum of given series.
Here is the code
<html>
<head>
<title>Find the Sum of given 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
function Series($x, $n)
{ $sum = 1;
$term = 1;
$fact = 1;
$pow = 1;
$multi = 1;
// Computing sum of remaining n-1 terms.
for ($i = 1; $i < $n; $i++) {
$fact = $fact * $multi * ($multi+1);
$pow = $pow * $x * $x;
$term = (-1) * $term;
$multi + = 2;
$sum = $sum + ($term * $pow)/$fact;
}
return $sum;
}
if(isset($_POST[ 'submit']))
{
$x = $_POST[ 'num1'];
$n = $_POST[ 'num2'];
$precision = 4;
echo "X value : ".$x."</br>";
echo "N value : ".$n."</br>";
echo "Sum of given series is : " .number_format(Series($x, $n), $precision);
return 0;
}
?>
</body>
</html>