Created
March 14, 2022 17:24
-
-
Save joshuamkite/4f008332a5b677e8f6dd914c7ec7fd16 to your computer and use it in GitHub Desktop.
Fizzbuzz implementation in Golang
This file contains 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
// Print integers 1 to 100, but print “Fizz” if an integer is divisible by 3, “Buzz” if an integer is divisible by 5, and “FizzBuzz” if an integer is divisible by both 3 and 5. | |
// This implementation populate our integers using a for loop and uses nested if statements but by using boolean evaluations at the start of our evaluation loop we perform each modulus evaluation ony once | |
package main | |
import "fmt" | |
func main() { | |
n := []int{} | |
for i := 1; i <= 100; i++ { | |
n = append(n, i) | |
} | |
for _, j := range n { | |
p := (j%3 == 0) | |
q := (j%5 == 0) | |
if p && q { | |
fmt.Println("FizzBuzz") | |
} else { | |
if p { | |
fmt.Println("Fizz") | |
} else { | |
if q { | |
fmt.Println("Buzz") | |
} else { | |
fmt.Println(j) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment