Skip to content

Instantly share code, notes, and snippets.

@raydvard
Created October 5, 2014 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raydvard/0c69cb1ca89f29b97785 to your computer and use it in GitHub Desktop.
Save raydvard/0c69cb1ca89f29b97785 to your computer and use it in GitHub Desktop.
assignment-2 (prime,even and odd) // PHP
<?php
if(isset($_POST["submit"]))
{
// if submitted assign variables values
$low = $_POST["lowNum"];
$highLimit = $_POST["highNum"];
$countLines = 0;
$sum_ev = 0;
$sum_od = 0;
$sum_prm = 0;
$constant_prm;
}
// Function for Prime Numbers Queries
function primePrint()
{
echo "#==================Prime Numbers=========================#";
global $low, $highLimit, $lowLimit, $countLines, $sum_prm;
for( $lowLimit = $low; $lowLimit <= $highLimit; $lowLimit++ )
{
for( $constant_prm = 2; $constant_prm <= $lowLimit - 1; $constant_prm++ )
{
if( $lowLimit % $constant_prm == 0 )
{
break;
}
}
if( $constant_prm == $lowLimit )
{
$countLines++;
echo "<br>"."Prime Number ".$countLines." : ".$lowLimit;
$sum_prm = $sum_prm + $lowLimit;
}
}
echo "<br>";
echo "<strong>Total Prime Numbers Count is : </strong>".$countLines;
echo "<br>";
echo "<strong>Total Prime Numbers Summation is : </strong>".$sum_prm;
echo "<br>";
echo "#=====================================================#";
}
// Function for Even Numbers Queries
function evenPrint()
{
echo "#==================Even Numbers=========================#";
global $low, $highLimit, $lowLimit, $countLines, $sum_ev;
for( $lowLimit = $low; $lowLimit <= $highLimit; $lowLimit++ )
{
if( $lowLimit % 2 == 0 )
{
$countLines++;
echo "<br>"."Even Number ".$countLines." : ".$lowLimit;
$sum_ev = $sum_ev + $lowLimit;
}
}
echo "<br>";
echo "<strong>Total Even Numbers Count is : </strong>".$countLines;
echo "<br>";
echo "<strong>Total Even Numbers Summation is : </strong>".$sum_ev;
echo "<br>";
echo "#=====================================================#";
}
// Function for Odd Numbers Queries
function oddPrint()
{
echo "#==================Odd Numbers=========================#";
global $low, $highLimit, $lowLimit, $countLines, $sum_od;
for( $lowLimit = $low, $countLines = 0; $lowLimit <= $highLimit; $lowLimit++ )
{
if( $lowLimit % 2 > 0 )
{
$countLines++;
echo "<br>"."Odd Number ".$countLines." : ".$lowLimit;
$sum_od = $sum_od + $lowLimit;
}
}
echo "<br>";
echo "<strong>Total Odd Numbers Count is : </strong>".$countLines;
echo "<br>";
echo "<strong>Total Odd Numbers Summation is : </strong>".$sum_od;
echo "<br>";
echo "#=====================================================#";
}
?>
<?php
// Form to get inputs
echo "<form action='assignment-2.php' method='post'>";
echo "Enter the range of numbers to find prime, even and odd numbers >>";
echo "<br>";
echo "<br>";
echo "Enter the lower range number : "."<input type='text' name='lowNum'>";
echo "<br>";
echo "Enter the higher range number : "."<input type='text' name='highNum'>";
echo "<br>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
?>
<?php
echo "<br>";
if(isset($_POST["submit"]))
{
// if submitted then call the functions
primePrint();
echo "<br>";
evenPrint();
echo "<br>";
oddPrint();
}
?>
@raydvard
Copy link
Author

raydvard commented Oct 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment