CracklePop in PHP
Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Crackle instead of the number. If itβs divisible by 5, print Pop. If itβs divisible by both 3 and 5, print CracklePop. - recurse.com
CracklePop is a common interview question. The following is one way of doing it:
for ($i = 1; $i <= 100; $i++) {
if ($i % 3 === 0) {
echo 'Crackle';
}
if ($i % 5 === 0) {
echo 'Pop';
}
if ($i % 3 !== 0 && $i % 5 !== 0) {
echo $i;
}
echo "\n"; //new line
}
Written on February 9, 2016