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("");
        }

        Console.ReadKey();

    }
}

}

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 :slight_smile: 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}
}