Skip to content

Instantly share code, notes, and snippets.

@4E71
Created September 17, 2012 02:12
Show Gist options
  • Save 4E71/3735193 to your computer and use it in GitHub Desktop.
Save 4E71/3735193 to your computer and use it in GitHub Desktop.
GoLang: "fizzbuzz" is the new "hello world"
// 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)
}
}
// 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