PHP | Write a program to compute the sum of first ‘N’ terms (n>=1) of the series 1-3+5-7+9…………………… Write a program to compute the sum of first ‘N’ terms (n>=1) of the series 1-3+5-7+9…………………… Email ThisBlogThis!Share to TwitterShare to Facebook Reactions: :Labs 4 Comments
$N=$_POST["num"];
ReplyDelete$sum=0;
for($i=1,$k=0;$i<=2*$N-1;$i+=2,$k++)
{
if($k%2==0)
$sum+=$i;
else
$sum-=$i;
}
echo "$sum";
php
ReplyDelete$num1=$_POST['num'];
$sum=0;
for($i=1;$i<=$num1;$i+=4)
{$sum=$sum+$i;
}
$sum1=0;
for($j=3;$j<=$num1;$j+=4)
{$sum1=$sum1+$j;
}
$result=$sum-$sum1;
echo"$result";
?>
$N=$_POST["n"];
ReplyDelete$sum=0;
for($j=0,$i=1;$i<=2*$N-1;$j++,$i+=2)
{
if($j%2==0)
$sum+=$i;
else
$sum-=$i;
}
echo "$sum";
php
ReplyDelete$num=$_post["$s"];
$total=0;
for($i=0,$j=1;$j<=2*$s-1;$i++,$j=$j+2)
{
if ($i%2==0)
{
$total=$total+$j;
}
else
{$total=$total-$j;
}
echo"$total";