You are viewing our Forum Archives. To view or take place in current topics click here.
FizzBuzzzz Program
Posted:
FizzBuzzzz ProgramPosted:
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
What are some ways of implementing FizzBuzz?
Description:
My Implementations in PHP
- Implementation 1 (DRY)
- Implementation 2 (without If / Else Statements)
Last edited by Cyimking ; edited 4 times in total
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:
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year 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.
I'm missing the part where it prints "Fizz" for numbers that are solely multiples of 3.
- 2useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
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; ?>
- 1useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
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");
}
- 1useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Nov 02, 200915Year Member
Posts: 3,853
Reputation Power: 149
Status: Offline
Joined: Nov 02, 200915Year 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
- 1useful
- 0not useful
#6. Posted:
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year 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.
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year 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.
- 0useful
- 0not useful
#8. Posted:
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
Cyimking wroteCriticaI 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
- 1useful
- 0not useful
#9. Posted:
Status: Offline
Joined: Sep 26, 201014Year Member
Posts: 401
Reputation Power: 17
Status: Offline
Joined: Sep 26, 201014Year 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);
}
}
}
- 1useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.