PHP Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 12: Write a PHP program that finds out the sum of first n odd numbers.

odd_Sum.php

<!DOCTYPE html>
<html>
<head>
	<title>PHP Programs TutorialDuniya.com</title>
</head>
<body>
	<form method="GET" action="">
		Enter Number: <input type="Text" name="num1"><br/>
		<input type="Submit" name="submit" value="Odd_sum">
	</form>

	<?php
		if(isset($_GET["submit"]) == "Odd_sum")
		{
			$sum =0;
			$var1 =$_GET["num1"];
			echo"<br/>First ".$var1."odd number is : ";
			for ($i=1; $i <=2*$var1 ; $i++)
			{
				if($i%2!=0)
				{
					echo $i."";
					$sum =$sum+$i;
				}
			}
			echo"<br/>Sum of first ".$var1."odd number is : ";
			echo $sum;
		} 
	?>
</body>
</html>



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.