Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
docs:programming:fizzbuzz [2007/07/07 23:59] billhdocs:programming:fizzbuzz [2008/08/03 00:25] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== FizzBuzz ======
 +  * http://www.codinghorror.com/blog/archives/000781.html
  
 +===== FizzBuzz Problem 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".//
 +
 +===== C =====
 +**FizzBuzz.c**
 +<code c>
 +#include <stdio.h>
 +
 +int main(int argc, const char *argv[]){
 + int i;
 +
 + for(i=1; i<=100; i++){
 + if(i % 3 == 0 && i % 5 == 0){
 + printf("FizzBuzz\n");
 + }else if(i % 3 == 0){
 + printf("Fizz\n");
 + }else if(i % 5 == 0){
 + printf("Buzz\n");
 + }else{
 + printf("%d\n", i);
 + }
 + }
 +
 + return 0;
 +}
 +</code>
 +  * build<code>
 +gcc -o FizzBuzz FizzBuzz.c
 +</code>
 +  * run<code>
 +./FizzBuzz
 +</code>
 +
 +===== C++ =====
 +FIXME
 +
 +===== C# =====
 +FIXME
 +
 +===== Java =====
 +**FizzBuzz.java**
 +<code java>
 +public class FizzBuzz{
 +
 + public static void main(String args[]){
 + int i;
 +
 + for (i=1; i<=100; i++){
 + if(i % 3 == 0 && i % 5 == 0){
 + System.out.println("FizzBuzz");
 + }else if(i % 3 == 0){
 + System.out.println("Fizz");
 + }else if(i % 5 == 0){
 + System.out.println("Buzz");
 + }else{
 + System.out.println(i);
 + }
 + }
 + }
 +
 +}
 +</code>
 +  * build<code>
 +javac FizzBuzz.java
 +</code>
 +  * run<code>
 +java FizzBuzz
 +</code>
 +
 +
 +===== Perl =====
 +**FizzBuzz.pl**
 +<code perl>
 +#!/usr/bin/perl -w
 +
 +my $i;
 +
 +for($i=1; $i<=100; $i++){
 + if($i % 3 == 0 && $i % 5 == 0){
 + print "FizzBuzz\n";
 + }elsif($i % 3 == 0){
 + print "Fizz\n";
 + }elsif($i % 5 == 0){
 + print "Buzz\n";
 + }else{
 + print $i . "\n";
 + }
 +}
 +</code>
 +  * run<code>
 +perl FizzBuzz.pl
 +</code>
 +
 +===== Visual Basic =====
 +<code vb>
 +Public Sub FizzBuzz()
 +    Dim i As Integer
 +
 +    For i = 1 To 100
 +        If (i Mod 3 = 0 And i Mod 5 = 0) Then
 +            Debug.Print "FizzBuzz"
 +        ElseIf (i Mod 3 = 0) Then
 +            Debug.Print "Fizz"
 +        ElseIf (i Mod 5 = 0) Then
 +            Debug.Print "Buzz"
 +        Else
 +            Debug.Print i
 +        End If
 +    Next
 +End Sub
 +</code>
 +
 +
 +===== Objective-C (Mac) =====
 +**FizzBuzz.m**
 +<code objc>
 +#import <Foundation/Foundation.h>
 + 
 +@interface FizzBuzz : NSObject{
 +}
 +- (void)output;
 +@end
 + 
 +@implementation FizzBuzz
 +
 +- (void)output{
 + int i;
 +
 + for(i=1; i<=100; i++){
 + if(i % 3 == 0 && i % 5 == 0){
 + printf("FizzBuzz\n");
 + }else if(i % 3 == 0){
 + printf("Fizz\n");
 + }else if(i % 5 == 0){
 + printf("Buzz\n");
 + }else{
 + printf("%d\n",i);
 + }
 + }
 +}
 +
 +@end
 +
 +int main(int argc, const char *argv[]){
 + FizzBuzz *fb = [[FizzBuzz alloc] init];
 + [fb output];
 + [fb release];
 +
 + return 0;
 +}
 +</code>
 +
 +  * build<code>
 +gcc -ObjC -framework Foundation -o FizzBuzz FizzBuzz.m
 +</code>
 +  * run<code>
 +./FizzBuzz
 +</code>
 +
 +===== PHP =====
 +**FizzBuzz.php**
 +<code php>
 +<?php
 + $i;
 +
 + for($i = 1; $i<=100; $i++){
 + if($i % 3 == 0 && $i % 5 == 0){
 + echo "FizzBuzz\n";
 + }else if($i % 3 == 0){
 + echo "Fizz\n";
 + }else if($i % 5 == 0){
 + echo "Buzz\n";
 + }else{
 + echo "$i\n";
 + }
 + }
 +
 +?>
 +</code>
 +  * run<code>
 +php FizzBuzz.php
 +</code>
 +
 +
 +===== Javascript/ECMAScript =====
 +**FizzBuzz.htm**
 +<code javascript>
 +<script type="text/javascript">
 + var i;
 +
 + for(i=1; i<=100; i++){
 + if(i % 3 == 0 && i % 5 == 0){
 + document.write("FizzBuzz<br />");
 + }else if(i % 3 == 0){
 + document.write("Fizz<br />");
 + }else if(i % 5 == 0){
 + document.write("Buzz<br />");
 + }else{
 + document.write(i + "<br />");
 + }
 + }
 +</script>
 +</code>
 +
 +===== Shell Script =====
 +FIXME
 +
 +===== Correct Output (common with all languages) =====
 +<code>
 +1
 +2
 +Fizz
 +4
 +Buzz
 +Fizz
 +7
 +8
 +Fizz
 +Buzz
 +11
 +Fizz
 +13
 +14
 +FizzBuzz
 +16
 +17
 +Fizz
 +19
 +Buzz
 +Fizz
 +22
 +23
 +Fizz
 +Buzz
 +26
 +Fizz
 +28
 +29
 +FizzBuzz
 +31
 +32
 +Fizz
 +34
 +Buzz
 +Fizz
 +37
 +38
 +Fizz
 +Buzz
 +41
 +Fizz
 +43
 +44
 +FizzBuzz
 +46
 +47
 +Fizz
 +49
 +Buzz
 +Fizz
 +52
 +53
 +Fizz
 +Buzz
 +56
 +Fizz
 +58
 +59
 +FizzBuzz
 +61
 +62
 +Fizz
 +64
 +Buzz
 +Fizz
 +67
 +68
 +Fizz
 +Buzz
 +71
 +Fizz
 +73
 +74
 +FizzBuzz
 +76
 +77
 +Fizz
 +79
 +Buzz
 +Fizz
 +82
 +83
 +Fizz
 +Buzz
 +86
 +Fizz
 +88
 +89
 +FizzBuzz
 +91
 +92
 +Fizz
 +94
 +Buzz
 +Fizz
 +97
 +98
 +Fizz
 +Buzz
 +</code>