Created
September 17, 2012 02:12
-
-
Save 4E71/3735193 to your computer and use it in GitHub Desktop.
GoLang: "fizzbuzz" is the new "hello world"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fizzbuzz | |
// A program that prints the numbers from 1 to n. 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”. | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
fmt.Print("Enter integer: ") | |
var input int | |
fmt.Scanf("%d", &input) | |
for i := 1; i <= input; i++ { | |
fizzbuzz(i) | |
} | |
} | |
func fizzbuzz(i int) { | |
fizz := "fizz" | |
buzz := "buzz" | |
if i % 3 == 0 && i % 5 == 0 { | |
fmt.Println(i, fizz + buzz) | |
} else if i % 3 == 0 { | |
fmt.Println(i, fizz) | |
} else if i % 5 == 0 { | |
fmt.Println(i, buzz) | |
} else { | |
fmt.Println(i) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: go run fizzbuzz2.go -max=SOME_INT | |
package main | |
import ( | |
"fmt" | |
"flag" | |
) | |
var max *int = flag.Int("max", 0, "enter integer or bust!") | |
func main() { | |
flag.Parse() | |
for i := 1; i <= *max; i++ { | |
fizzbuzz(i) | |
} | |
} | |
func fizzbuzz(i int) { | |
fizz := "fizz" | |
buzz := "buzz" | |
if i % 3 == 0 && i % 5 == 0 { | |
fmt.Println(i, fizz + buzz) | |
} else if i % 3 == 0 { | |
fmt.Println(i, fizz) | |
} else if i % 5 == 0 { | |
fmt.Println(i, buzz) | |
} else { | |
fmt.Println(i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment