[Golang]BMI

I am using fmt.Scanln() to read user’s input, the reader stops at newline, on previous case I use fmt.Scanf() but it reads the first input and terminates.

This is part of an exercise for my golang tutorial.

package main

import "fmt"

func main(){
	var weight float64
	var height float64
	fmt.Println("Your weight: ")
	fmt.Scanln(&weight)
	fmt.Println("Your height: ")
	fmt.Scanln(&height)
	switch bmi := getBMI(height, weight); {
	case bmi < 18.5:
		fmt.Printf("Your bmi is %.2f, and you are underweight.", bmi)
	case bmi < 25:
		fmt.Printf("Your bmi is %.2f, and your BMI is normal", bmi)
	case bmi < 30:
		fmt.Printf("Your bmi is %.2f, and you are overweight", bmi)
	default:
		fmt.Printf("Your bmi is %.2f, and you are obese.", bmi)
	}
}

func getBMI(height float64, weight float64) float64 {
	return weight / (height * height)
}

bmi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s