FizzBuzz questions & coding for fun!

Two days ago I was reading in RethinkDB‘s blog the “Will the real programmers please stand up?” which took me to the blogpost of Coding Horror “Why Can’t Programmers.. Program?”.

After reading these two articles I had the idea to start a FizzBuzz coding section just for fun (CfFCoding for Fun).
So today’s FizzBuzz question is…

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Below is my solution written in php.

<?php
echo '<h2>Coding For Fun</h2>';
$three = 3;
$five = 5;
$fifteen =15;
for ($i=1; $i<=100; $i++){
if (($i%$three === 0) && ($i%$five === 0)) {
echo 'FizzBuzz<br />';
} elseif ($i%$three === 0) {
echo 'Fizz<br />';
} elseif ($i%$five === 0) {
echo 'Buzz<br />';
} else {
echo $i . '<br />';
}
}
echo '<br />==============<br /><br />';
for ($i=1; $i<=100; $i++){
if ($i%$fifteen === 0) {
echo 'FizzBuzz<br />';
} elseif ($i%$three === 0) {
echo 'Fizz<br />';
} elseif ($i%$five === 0) {
echo 'Buzz<br />';
} else {
echo $i . '<br />';
}
}
?>

Yes… it took me 10 minutes 🙁
Can i stand up?!
😉

1 Star2 Stars (+12 rating, 3 votes)
Loading...
  • Amy
    for($i=1; $i 0?$i:(!$i%3==0&amp;&amp;$i%5==0?'FizzBuzz':($i%5==0?'Buzz':'Fizz'))).'';    
    }
    
    • Amy

      bah. stupid striptags.

      for($i=1; $i 0?$i:(!$i%3==0&amp;&amp;$i%5==0?'FizzBuzz':($i%5==0?'Buzz':'Fizz'))).'&lt;br /&gt;';    
      }
      
  • dave

    How would u use with html
    Like having 2 files index.htlm and fizzbuzz.php any suggestions.Am new with php

    • Dave, could you please explain exactly what do you want to do ?!

  • Gregor

    Hi! I don’t mean to offend you but your solution shows that you’re a bad programmer. You should allways try to find a better solution that the one with many conditional statements i.e. the most obvious one.
    Also, I don’t know why you prefere to use variables instead of numbers. You could define $a and $b as variables in order to reuse your code with other numbers but in such case you certainly don’t need a third variable because is calculated out of the two previous ones.
    Finally, by using a chain of if elseif statements instead of sepatate if conditions, you need one more condition than it would be actually necessary.
    I urge you to think about how many lines of code you write and how efficient it is rather than how much time it took you to write it.
    Finally, here’s my solution using ternary operators in five lines of code.
    <?php
    for($i=1;$i
    By using classic conditional statements (though there’s no need for them), the code would look like:
    <?php
    for($i=1;$i
    Allways think about code efficiency no matter how small the script is. This is the real challenge in programming and this will make you a great programmer.
    Good luck!

  • Sorry, look’s like your code editor is broken.
    You can find both sample codes here
    http://codepad.viper-7.com/UhxN0W
    Best regards!
    Gregor