126. PHP Program to Compute the Given Series 1/1 + 1/3 + 1/5 + 1/7 + ……. + 1/n
How does this program work?
- This program is used to find the Sum of the given series 1/1 + 1/3 + 1/5 + 1/7 + ……. + 1/n using PHP.
- The integer entered by the user is stored in variable 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 n value"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num1'];
$s = 0.0;
$precision =4;
for ($i = 1; $i <= $n; $i+=2)
{
$s = $s + 1 / $i;
}
echo "Sum of given series is : " .number_format($s, $precision);
return 0;
}
?>
</body>
</html>