====== 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** #include 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; } * build gcc -o FizzBuzz FizzBuzz.c * run ./FizzBuzz ===== C++ ===== FIXME ===== C# ===== FIXME ===== Java ===== **FizzBuzz.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); } } } } * build javac FizzBuzz.java * run java FizzBuzz ===== Perl ===== **FizzBuzz.pl** #!/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"; } } * run perl FizzBuzz.pl ===== Visual Basic ===== 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 ===== Objective-C (Mac) ===== **FizzBuzz.m** #import @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; } * build gcc -ObjC -framework Foundation -o FizzBuzz FizzBuzz.m * run ./FizzBuzz ===== PHP ===== **FizzBuzz.php** * run php FizzBuzz.php ===== Javascript/ECMAScript ===== **FizzBuzz.htm** ===== Shell Script ===== FIXME ===== Correct Output (common with all languages) ===== 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