FizzBuzz problem in Go
Requirements
- Print Integer 1 to 50, except:
- Print “Fizz” if the number is divisible by 3.
- Print “Buzz” if the number is divisible by 5.
- Print “FizzBuzz” if the number is divisible by 3 and 5.
ideally, we should write the logic for the lcm of two numbers
but since its already given 3,5
so I directly used their lcm.
Also, we are placing this logic at the top
because “fizz” and “buzz” will both execute if we place it lower.
//fizzBuzz problems
func fizzBuzzProblem() {
for i := 1; i <= 50; i++ {
if i%15 == 0 {
/*
ideally we should write logic for the lcm of two numbers
but since its already given 3,5
so i directly used their lcm.
Also we are placing this logic at the top
because “fizz” and “buzz” will both execute if we place it lower.
*/
fmt.Println(“FizzBuzz”)
} else if i%3 == 0 {
fmt.Println(“Fizz”)
} else if i%5 == 0 {
fmt.Println(“Buzz”)
} else {
fmt.Println(i)
}
}
}
Fizz buzz implementation in other programming language is as follows.