Skip to content

Instantly share code, notes, and snippets.

@genghisjahn
Last active Feb 11, 2022
Embed
What would you like to do?
Number Guessing Game
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var n int
var g int
var c int
rand.Seed(int64(time.Now().Nanosecond()))
n = rand.Intn(100)
for {
fmt.Printf("Enter your guess: ")
fmt.Scanln(&g)
c = c + 1
if g == n {
fmt.Println("You guessed it!")
fmt.Println("No. Guesses: ", c)
break
} else if g > n {
fmt.Println("Too high.")
} else {
fmt.Println("Too low.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment