Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
docs:programming:fizzbuzz [2007/07/07 23:59] – billh | docs:programming:fizzbuzz [2008/08/03 00:25] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== FizzBuzz ====== | ||
+ | * http:// | ||
+ | ===== FizzBuzz Problem Description ===== | ||
+ | //Write a program that prints the numbers from 1 to 100. But for multiples of three print " | ||
+ | |||
+ | ===== C ===== | ||
+ | **FizzBuzz.c** | ||
+ | <code 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(" | ||
+ | }else if(i % 3 == 0){ | ||
+ | printf(" | ||
+ | }else if(i % 5 == 0){ | ||
+ | printf(" | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | * build< | ||
+ | gcc -o FizzBuzz FizzBuzz.c | ||
+ | </ | ||
+ | * run< | ||
+ | ./FizzBuzz | ||
+ | </ | ||
+ | |||
+ | ===== 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(" | ||
+ | }else if(i % 3 == 0){ | ||
+ | System.out.println(" | ||
+ | }else if(i % 5 == 0){ | ||
+ | System.out.println(" | ||
+ | }else{ | ||
+ | System.out.println(i); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | * build< | ||
+ | javac FizzBuzz.java | ||
+ | </ | ||
+ | * run< | ||
+ | java FizzBuzz | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Perl ===== | ||
+ | **FizzBuzz.pl** | ||
+ | <code perl> | ||
+ | # | ||
+ | |||
+ | my $i; | ||
+ | |||
+ | for($i=1; $i<=100; $i++){ | ||
+ | if($i % 3 == 0 && $i % 5 == 0){ | ||
+ | print " | ||
+ | }elsif($i % 3 == 0){ | ||
+ | print " | ||
+ | }elsif($i % 5 == 0){ | ||
+ | print " | ||
+ | }else{ | ||
+ | print $i . " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | * run< | ||
+ | perl FizzBuzz.pl | ||
+ | </ | ||
+ | |||
+ | ===== 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 " | ||
+ | ElseIf (i Mod 3 = 0) Then | ||
+ | Debug.Print " | ||
+ | ElseIf (i Mod 5 = 0) Then | ||
+ | Debug.Print " | ||
+ | Else | ||
+ | Debug.Print i | ||
+ | End If | ||
+ | Next | ||
+ | End Sub | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Objective-C (Mac) ===== | ||
+ | **FizzBuzz.m** | ||
+ | <code objc> | ||
+ | #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(" | ||
+ | }else if(i % 3 == 0){ | ||
+ | printf(" | ||
+ | }else if(i % 5 == 0){ | ||
+ | printf(" | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @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** | ||
+ | <code php> | ||
+ | <?php | ||
+ | $i; | ||
+ | |||
+ | for($i = 1; $i<=100; $i++){ | ||
+ | if($i % 3 == 0 && $i % 5 == 0){ | ||
+ | echo " | ||
+ | }else if($i % 3 == 0){ | ||
+ | echo " | ||
+ | }else if($i % 5 == 0){ | ||
+ | echo " | ||
+ | }else{ | ||
+ | echo " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ?> | ||
+ | </ | ||
+ | * run< | ||
+ | php FizzBuzz.php | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Javascript/ | ||
+ | **FizzBuzz.htm** | ||
+ | <code javascript> | ||
+ | <script type=" | ||
+ | var i; | ||
+ | |||
+ | for(i=1; i<=100; i++){ | ||
+ | if(i % 3 == 0 && i % 5 == 0){ | ||
+ | document.write(" | ||
+ | }else if(i % 3 == 0){ | ||
+ | document.write(" | ||
+ | }else if(i % 5 == 0){ | ||
+ | document.write(" | ||
+ | }else{ | ||
+ | document.write(i + "< | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 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 | ||
+ | </ |