a companion discussion area for blog.codinghorror.com

# FizzBuzz Solution Dumping Ground

#141

Some old-school COBOL code (procedure division only):

000-MAINLINE

``````PERFORM VARYING WS-INT FROM 1 BY 1
UNTIL WS-INT > 100

DIVIDE WS-INT BY 3 GIVING WS-RESULT3 REMAINDER WS-REM3
DIVIDE WS-INT BY 5 GIVING WS-RESULT5 REMAINDER WS-REM5

IF WS-REM3 = 0
IF WS-REM5 = 0
MOVE 'FIZZBUZZ' TO WS-OUT
ELSE
MOVE 'FIZZ' TO WS-OUT
END-IF
ELSE
IF WS-REM5 = 0
MOVE 'BUZZ' TO WS-OUT
ELSE
MOVE WS-INT TO WS-OUT
END-IF
END-IF

WRITE OUTREC FROM WS-OUT
MOVE SPACES TO WS-OUT

END-PERFORM.

STOP RUN.
``````

#142

//I hardly consider myself above averageâŚ wowâŚ very sad!
//off the top of my head i figured i try it outâŚ works just fineâŚ
public class fizzBuzz
{ public static void main(String[] args)
{
String[] x= new String[100];
for (int i=0; i<100; i++)
if (i%3==0 && i%5==0)x[i]=âFizzBuzzâ;
else if (i%3==0)x[i]=âFizzâ;
else if (i%5==0)x[i]=âBuzzâ;
else x[i]=Integer.toString(i);
for (int i=0; i<100; i++) System.out.print(x[i] + ", ");
}
}

#143

(i%3==0?(i%5==0?âFizzBuzzâ:âFizzâ):(i%5==0?âBuzzâ:i))

#144

Haha, I just whipped this up in VBScript.
Iâm @ Durham College in Ontario, Canada and am taking Computer Systems.
My program is focused on networking and server administration, the other half of us are in the Computer Programmer side of things.
However, we take a few programming here and there, mostly intro to VB and C++, as well as batch/shell scripting and PHP/VBScript.
It saddens me that I, a beginner programmer (in my opinion), can easily whip this code snippet up in about 5min and be more qualified than 99% of âprogrammersâ.
Anyways, hereâs my snippet in VBScript, Iâm sure it would work in VB.NET as well.

â fizzbuzz.vbs
Dim i
Const FIZZ=3, BUZZ=5
i = 1

For i = 1 To 100
If i Mod FIZZ = 0 Then
Wscript.Echo "Fizz"
ElseIf i Mod BUZZ = 0 Then
Wscript.Echo "Buzz"
ElseIf i Mod (FIZZ+BUZZ) = 0 Then
Wscript.Echo "FizzBuzz"
Else
Wscript.Echo i
End If
Next

#145

python fizzbuzz

for x in range(1,101):
print ((x%3 and x%5) and x) or ((not(x % 3) and âfizzâ) or ââ) + ((not(x % 5) and âbuzzâ) or ââ)

#146

public class FizzBuzz {
public static void main(String args[]) {
for (int i = 1; i < 101; i++) {
if ((i % 3 == 0) && (i % 5 == 0)) {
System.out.println(âFizzBuzz\nâ);
} else if (i % 3 == 0) {
System.out.println(âFizz\nâ);
} else if (i % 5 == 0) {
System.out.println(âBuzz\nâ);
} else {
System.out.println(i + â\nâ);
}
}
}
}

woop woopâŚ man, incompetent âŚ [i was going to say programmers]people

#147

FizzBuzz in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

``````        for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0)
{
Console.Write("Fizz");
}
if (i % 5 == 0)
{
Console.Write("Buzz");
}

if (i % 5 > 0 && i % 3 > 0)
{
Console.Write(i);
}
Console.WriteLine("");
}

}
}
``````

}

My PHP port:

for(\$i = 1; \$i <= 100; \$i++){

``````if(\$i % 3 == 0){
echo 'Fizz';
}
if(\$i % 5 == 0){
echo 'Buzz';
}
if(\$i % 5 > 0 && \$i % 3 > 0){
echo \$i;
}
echo "\n";
``````

}

My Javascript Port:

for(var i = 1; i <= 100; i++){
var str = ââ;
if(\$i % 3 == 0){
str += âFizzâ;
}
if(\$i % 5 == 0){
str += âBuzzâ;
}
if(\$i % 5 > 0 && \$i % 3 > 0){
str += \$i;
}
console.log(str+"\n");
}

Jack of all trades is way better than master of one.

#148

Only a hobbyist, but here is my Google Go solution:

package main
import fmt âfmt"
func main() {
for i := 1; i <= 100; i++ {
switch {
case i % 15 == 0 :
fmt.Printf(âFizzBuzzâ)
case i % 3 == 0 :
fmt.Printf(âFizzâ)
case i % 5 == 0 :
fmt.Printf(âBuzzâ)
default:
fmt.Printf(â%d", i)
}
fmt.Printf("\n")
}
}

#149

for(\$i=0;\$i<100;)echo(++\$i%15?(\$i%5?(\$i%3?\$i:âfizzâ):âbuzzâ):âfizzbuzzâ);
73 Chars in PHP. Less anyone?

#150

âŚfor old timesâs sakeâŚ

program FizzBuzz;

uses SysUtils;

var
i: integer;

begin
for i:=1 to 100 do begin
if (i mod 15 = 0) then
WriteLn(âFizzBuzzâ)
else if (i mod 3 = 0) then
WriteLn(âFizzâ)
else if (i mod 5 = 0) then
WriteLn(âBuzzâ)
else WriteLn(IntToStr(i));
end;
end;

#151

I know that the algorithm itself itâs not the matter here, but here is the fizzbuzz ruby implementation (just for fun):

def fizzbuzz(max); max.times {|i| j = i + 1; out = ââ; out << âfizzâ if j % 3 == 0; out << âbuzzâ if j % 5 == 0; puts out.empty? ? j : out}; end

#152

one line of javascript

for(i=1;i<101;i++) document.write(((i%3==0 && i%5==0)?âFizzBuzzâ:(i%3==0)?âFizzâ:(i%5==0)?âBuzzâ:i) + â,â)

#153

Enumerable.Range(1, 100).ToList().ForEach(i => Console.WriteLine(i % 3 == 0 || i % 5 == 0 ? string.Concat( (i % 3 == 0 ? âFizzâ : ââ), (i % 5 == 0 ? âBuzzâ : ââ)) : i.ToString()));

#154

Just throwing in my 2 cents for python (even though weâre getting âyelledâ at for posting answers Iâm new as hell to python (few months) and I got the problem in ~5 minutes of tinkering.

```for i in range (1,101):
if (i % 3) == 0 and (i % 5) == 0:
print 'fizzbang'
elif i % 3 == 0:
print 'fizz'
elif i % 5 == 0:
print 'bang'
else:
print i
```

#155

in T-SQL, for completenessâŚ

```declare @i int
set @i = 1

while @i <= 100
begin
if (@i % 15 = 0) print 'FizzBuzz'
else if (@i % 5 = 0) print 'Buzz'
else if (@i % 3 = 0) print 'Fizz'
else print @i

set @i = @i + 1
end
```

#156

#include <stdio.h>
#include <stdlib.h>

#define F âFizzâ
#define B âBuzzâ
#define FB âFizzBuzzâ
#define null 0L

const char *tab[5][3]={
{FB,F,F},
{B, null, null},
{B, null, null},
{B, null, null},
{B, null, null} };

main(){
int i;
for (i=1;i<100;i++){
char buf[8];
sprintf( buf, â%-3dâ, i);
printf( â%s\nâ, (tab[i%5][i%3]?tab[i%5][i%3]:buf) );
}
}

Do I get the job ?

#157

C++

```#include

int main(int argc, char* argv[]){

const int MAX = 100;

for(int i = 1; i <= MAX; i++){
if(i % 3 == 0 && i % 5 == 0)
std::cout << "FizzBuzz" << std::endl;
else if(i % 3 == 0)
std::cout << "Fizz" << std::endl;
else if(i % 5 == 0)
std::cout << "Buzz" << std::endl;
else
std::cout << i << std::endl;
}

return 0;

}```

#158

It didnât really took too long to write this (adobe flash AS3)

for (var i:uint = 1; i <= 100; i++)
{
var fizz:Boolean = !(i % 5);
var buzz:Boolean = !(i % 3);
trace(buzz && fizz ? âFizzBuzzâ : fizz ? âFizzâ : buzz ? âBuzzâ : i);
}

#159
```/* cc -std=c99 main.c -o fizzbuzz */
#include <stdio.h>
int
main(int argc, char *argv[])
{
for (int i = 1; i < 101; i++) {
if (i % 3 == 0)
printf("Fizz");
if (i % 5 == 0)
printf("Buzz");
if (i % 3 && i % 5)
printf("%d",i);
printf("\n");
}
}
```

#160

i gues that mod (%) is not cheap operationâŚ

int j=0;
int k=0;
for (int i=0;i<100;i++,j++,k++)
{
if (j>=3)
{ cout<<âFizzâ;j=0;}
if (k>=5)
{ cout<<âBuzzâ;k=0}
}