<?php
///Wap to find the sum of given series 2+4+6+8.......+500
using for loop.
$sum=0;
for($i=2;$i<=500;$i++)
{
if($i%2==0)
{
echo $i;
echo "\t";
$sum=$sum+$i;
}
}
echo "\n";
echo "\n";
echo "Total sum=".$sum;
?>
While loop:-
<?php
///Wap to find the sum of given series 2+4+6+8.......+500
using while loop.
$sum=0;
$i=2;
while($i<=500)
{
if($i%2==0)
{
echo $i;
echo "\t";
$sum=$sum+$i;
}
$i++;
}
echo "\n";
echo "sum=".$sum;
?>
Do-while loop:-
<?php
///Wap to find the sum of given series 2+4+6+8.......+500
using Do-while loop.
$sum=0;
$i=2;
do
{
if($i%2==0)
{
echo $i;
echo "\t";
$sum=$sum+$i;
}
$i++;
}
while($i<=500);
echo "\n";
echo "sum=".$sum;
?>
All output is same :-


No comments:
Post a Comment
theengineerschoice01@gmail.com