Skip to content

PHP Programs 6

Calculate sum of natural numbers from one to n number.

<html>
<head>
<title>p6</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter the number"/>
</td>
</tr>
<td> <input type="submit" name="submit" value="Submit"/>
</td>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$num = $_POST['num1'];
$ans = ($num * ($num+1)) /2;
echo "The sum of natural number till $num is $ans :</br>";     
}
?>
</body>
</html>

Output