Math That Makes Sense

Math That Makes Sense

Math That Makes Sense

Go does math like you'd expect, with one twist: it won't let you mix types.

Go has all the arithmetic operators:

example.go
sum := 10 + 3    // 13
diff := 10 - 3   // 7
prod := 10 * 3   // 30
quot := 10 / 3   // 3  (not 3.33!)
rem := 10 % 3    // 1

Wait, 10 / 3 is 3? Yes. When both numbers are integers, Go does integer division. It drops the decimal part entirely. No rounding. Just gone.

Want the real answer? Use floats:

example.go
result := 10.0 / 3.0  // 3.3333333333333335

Go is strict about types. You can't mix integers and floats in the same expression, you have to convert explicitly:

example.go
x := 10
y := 3.0
result := float64(x) / y  // 3.3333...

This feels annoying at first, but it prevents bugs. Go makes you say what you mean, every time.

>_Exercise

Exercise: Split the Bill

You're splitting a dinner bill. The starter code gives you:

  • total (a float64: 95.50)
  • people (an int: 4)

Calculate each person's share and print the result in this exact format:

example.go
Each person pays: $23.88
Stuck? Reveal a hint to help you.
Hints (0/3)
Key Takeaway
Next UpMaking your code choose between paths.