resolving this simple php math -
i trying multiple number of 2 , 3 pls help. thanks
for($i = 0; $i <30; $i++) if($i % 2) echo 'number '. $i . ' multiple of 2 <br/>'; elseif($i %3) echo 'number '. $i . 'is multiple of 3 <br/>'; else echo 'number '. $i . 'is multiple of other number <br/>';
try this:
if($i % 2 === 0) ... elseif($i %3 === 0) ...
basically if modulo 0
means number evenly divisible.
however, problem logic number may divisible both 2
, 3
. can fix extracting these out separate if
statements:
if($i %2 === 0) { ... } if($i %3 === 0) { ... }
but sort of breaks last else
since cannot fall though anymore. solve setting variable false
@ top of loop. if of if
statements triggered, set variable true
. finally, print "not divisible" message @ end of each iteration if variable still false
.
Comments
Post a Comment