View Full Version : Write a FizzBuzz program in a language of your choice in under 4 minutes
Agnostic Bear
01-07-2010, 08:08 PM
Loop from 1 to 100,
If the number is divisible by 3, it must print fizz.
If the number is divisible by 5, it must print buzz.
If the number is divisible by both 3 and 5 it must print fizzbuzz.
If divisible by neither, neither fizz or buzz should be printed.
Just a small something to waste some time.
Jamesy
02-07-2010, 09:00 AM
<?php
$Count = 1;
while ($Count < 101)
{
echo $Count;
if ($Count % 3 == 0)
{
echo " fizz";
}
if ($Count % 5 == 0)
{
echo " buzz";
}
echo "<br />";
$Count++;
}
?>
There are probably nicer ways of doing it I guess.
Jaiisun
02-07-2010, 09:46 AM
used turbo delphi:
var
count : integer;
begin
count := 1;
repeat
if count mod 3 = 0 then write ('fizz');
if count mod 5 = 0 then write ('buzz');
writeln;
count := count + 1;
until count = 101;
readln;
end.
Jonster
02-07-2010, 11:13 AM
Written in Python:
for number in range(1,101):
if number % 3 == 0:
if number % 5 == 0:
print(str(number)+" : fizzbuzz")
else:
print(str(number)+" : fizz")
elif number % 5 == 0:
if number % 3 == 0:
print(str(number)+" : fizzbuzz")
else:
print(str(number)+" : buzz")
else:
print(str(number)+" : --")Which outputs (first 15):
1 : --
2 : --
3 : fizz
4 : --
5 : buzz
6 : fizz
7 : --
8 : --
9 : fizz
10 : buzz
11 : --
12 : fizz
13 : --
14 : --
15 : fizzbuzz
MattFr
02-07-2010, 02:10 PM
Actionscript 3, messy but whatever :P
package
{
public class Application
{
public function Application()
{
for (var i:int = 1; i <= 100; i++)
{
trace(i);
if (i % 3 == 0)
{
trace("FIZZ");
}
if (i % 5 == 0)
{
trace("BUZZ");
}
trace("--");
}
}
}
}
Agnostic Bear
02-07-2010, 03:08 PM
<?php
$Count = 1;
while ($Count < 101)
{
echo $Count;
if ($Count % 3 == 0)
{
echo " fizz";
}
if ($Count % 5 == 0)
{
echo " buzz";
}
echo "<br />";
$Count++;
}
?>
There are probably nicer ways of doing it I guess.
Hahaha, I wrote this yesterday before posting the thread, eerily similar:
<?php
for( $i = 1; $i <= 100; $i++ )
{
echo $i . ' - ';
if( $i % 3 == 0 ) { echo 'Fizz'; }
if( $i % 5 == 0 ) { echo 'Buzz'; }
echo '<br />';
}
?>
Invent
02-07-2010, 05:15 PM
Just another way of doing it in PHP :P
for( $i = 1; $i <= 100; $i++ ) {
echo $i, ' - ', ( ( $i % 3 === 0 ) ? 'Fizz' : '' ), ( ( $i % 5 === 0 ) ? 'Buzz' : '' ), '<br />';
}
Jamesy
02-07-2010, 06:06 PM
It's getting smaller and smaller haha!
Hitman
02-07-2010, 08:08 PM
<?php fizzbuzzgame(1-100, 3, 5.print) ?>---------- Post added 02-07-2010 at 09:08 PM ----------
I tried yesterday, took me 10 minutes to get down 2 lines of ********. My PHP is not great.
Hitman
04-07-2010, 09:08 AM
And there is some big ass silence... mine, of course, was a joke. Anybody else got any other languages they can do this in? Javascript?
Apolva
04-07-2010, 10:51 AM
<script type='text/javascript'>
for(i=1;i<101;i++) document.write(i+": "+(i%3==0?'fizz':'')+(i%5==0?'buzz':'')+'<br />');
</script>
Hitman
04-07-2010, 11:15 AM
<script type='text/javascript'>
for(i=1;i<101;i++) document.write(i+": "+(i%3==0?'fizz':'')+(i%5==0?'buzz':'')+'<br />');
</script>
Cool stuff, you're good at coding.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.