Created
August 19, 2022 22:19
-
-
Save thiagozs/1b19a8f07ec8a20a56f4e61206a2d666 to your computer and use it in GitHub Desktop.
HackerRank FizzBuzz
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
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