d
Amit DhamuSoftware Engineer
 

Multiples Of

2 minute read 00000 views

Print the numbers from 1 to 100 but if the number is a multiple of 3, print "Multiple of 3"; if the number is a multiple of 5, print "Multiple of 5"; if the number is a multiple of 3 and 5, print "Multiple of 3 and 5", otherwise just print the number.

foreach(range(1, 100) as $num) {
    if ($num % 3 == false && $num % 5 == false) {
        echo "$num is a Multiple of 3 and 5";
    } else if($num % 3 == false) {
        echo "$num is a Multiple of 3";
    } else if($num % 5 == false) {
        echo "$num is a Multiple of 5";
    } else {
        echo $num;
    }
}

One of the shortest ways

while(++$i<101)echo $i%15?$i%5?$i%3?$i:'Fizz':'Buzz':'FizzBuzz',"\n";

Credit