Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created August 19, 2022 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagozs/1b19a8f07ec8a20a56f4e61206a2d666 to your computer and use it in GitHub Desktop.
Save thiagozs/1b19a8f07ec8a20a56f4e61206a2d666 to your computer and use it in GitHub Desktop.
HackerRank FizzBuzz
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
/*
* Complete the 'fizzBuzz' function below.
*
* The function accepts INTEGER n as parameter.
*/
func fizzBuzz(n int32) {
// Write your code here
fizz := "Fizz"
buzz := "Buzz"
for i := int32(1); i <= n; i++ {
if i % 3 == 0 && i % 5 == 0 {
fmt.Println(fizz + buzz)
} else if i % 3 == 0 {
fmt.Println(fizz)
} else if i % 5 == 0 {
fmt.Println(buzz)
} else {
fmt.Println(i)
}
}
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int32(nTemp)
fizzBuzz(n)
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment