Write php code that will calculate and print the sum of all numbers less than 100. If the sum is even, print “even” to the web page; otherwise, print “odd”.
1
Expert's answer
2013-03-07T10:18:03-0500
<?php $sum=0;
for($i=0;$i<=99;$i++){ $sum=$sum+$i;}
echo "The total was ", $sum," which is ";
if ($sum%2==0){ echo "even";}
if ($sum%2==1){ echo "odd";}
?>
The second If check is redundant because if a number is not even it is going to be odd so you could use:
Comments
Leave a comment