You are viewing our Forum Archives. To view or take place in current topics click here.
FizzBuzzzz Program
Posted:

FizzBuzzzz ProgramPosted:

Cyimking
  • 2 Million
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
What are some ways of implementing FizzBuzz?

Description:
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".


My Implementations in PHP

- Implementation 1 (DRY)



<?php

for($x = 1; $x < 101; $x++) {
    $fizz = !($x % 3) ? "Fizz" : "";
    $buzz = !($x % 5) ? "Buzz" : "";
    echo (($fizz || $buzz) ? $fizz.$buzz : $x) . "<br />";
}


- Implementation 2 (without If / Else Statements)


// Coming Soon...


Last edited by Cyimking ; edited 4 times in total
#2. Posted:
ObscureCoder
  • Resident Elite
Status: Offline
Joined: Jun 29, 201310Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201310Year Member
Posts: 211
Reputation Power: 13
I took a quick glance over your code.
I'm missing the part where it prints "Fizz" for numbers that are solely multiples of 3.
#3. Posted:
CriticaI
  • 2 Million
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
I think this is my favorite way.
<?php for ($i=1; $i < 100; $i++): ?>
  <?php if ($i % 5 == 0 && $i % 3 == 0): ?>
    <p>Fizzbuzz</p>
  <?php elseif ($i % 3 == 0): ?>
    <p>Fizz</p>
  <?php elseif ($i % 5 == 0): ?>
    <p>Buzz</p>
  <?php else: ?>
    <p><?php echo $i ?></p>
  <?php endif; ?>
<?php endfor; ?>
#4. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Here's a simple C implementation. (I study Java)


for (int i=0;i<101;i++) {
   if (i % 3 == 0) {
      System.out.print("Fizz");
   }
   if (i % 5 == 0) {
      System.out.print("Buzz");
   }
   if (i % 3 != 0 && i % 5 != 0) {
      System.out.print(i);
   }
   System.out.print("\n");
}
#5. Posted:
Holmesy
  • TTG Contender
Status: Offline
Joined: Nov 02, 200914Year Member
Posts: 3,853
Reputation Power: 149
Status: Offline
Joined: Nov 02, 200914Year Member
Posts: 3,853
Reputation Power: 149
is this for Cw or exam question, i had this question for my exam couple years ago for programing lol
#6. Posted:
Cyimking
  • V5 Launch
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
Holmesy wrote is this for Cw or exam question, i had this question for my exam couple years ago for programing lol



It's an interview question for a software job. lol I figured I see how other people are solving this simple problem.
#7. Posted:
Cyimking
  • TTG Senior
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201211Year Member
Posts: 1,129
Reputation Power: 34
CriticaI wrote I think this is my favorite way.
<?php for ($i=1; $i < 100; $i++): ?>
  <?php if ($i % 5 == 0 && $i % 3 == 0): ?>
    <p>Fizzbuzz</p>
  <?php elseif ($i % 3 == 0): ?>
    <p>Fizz</p>
  <?php elseif ($i % 5 == 0): ?>
    <p>Buzz</p>
  <?php else: ?>
    <p><?php echo $i ?></p>
  <?php endif; ?>
<?php endfor; ?>


Yup. Good solution but this isn't DRY enough.
#8. Posted:
CriticaI
  • Christmas!
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
Cyimking wrote
CriticaI wrote I think this is my favorite way.
<?php for ($i=1; $i < 100; $i++): ?>
  <?php if ($i % 5 == 0 && $i % 3 == 0): ?>
    <p>Fizzbuzz</p>
  <?php elseif ($i % 3 == 0): ?>
    <p>Fizz</p>
  <?php elseif ($i % 5 == 0): ?>
    <p>Buzz</p>
  <?php else: ?>
    <p><?php echo $i ?></p>
  <?php endif; ?>
<?php endfor; ?>


Yup. Good solution but this isn't DRY enough.


Didn't know you wanted it DRY
Your 1st implementation seams very dry btw
#9. Posted:
Bighair
  • Powerhouse
Status: Offline
Joined: Sep 26, 201013Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201013Year Member
Posts: 401
Reputation Power: 17
Was working on a project as I was browsing the forum, so gave it a go in JS


function fizzBuzz(){
    for( var i =0;i<101;i++){
        if( i % 5 == 0 && i % 3 == 0){
            console.log("Fizzbuzz");
        }else if(i % 3 == 0){
            console.log("Fizz")
        }else if(i % 5 == 0){
            console.log("Buzz")
        }else{
            console.log(i);
        }
    }
}
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.