116. PHP Program to Compute the 1+3+5+7+9+ …….+n Series.
How does this program work?
- This program is used to find the Sum of the given series 1+3+5+7+9+ …….+n using PHP.
- The integer entered by the user is stored in variable n i.e last term for the given series.
- By using for loop we can find the sum of series for odd numbers upto nth term.
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 nth number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num1'];
$sum = 0;
for ($i=1; $i<= $n; $i+=2)
//for loop to calculate series of odd numbers
{
$sum = $i+$sum;
// sum of odd numbers
}
echo "Sum of series for odd numbers upto $n numbers : ".$sum;
return 0;
}
?>
</body>
</html>