Start learning free

LESSON 17 OF 671 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in Go

Lesson

  • We can perform multiple calculations using operators in the same line of code:

    fmt.Println(3 - 4 + 6) // Prints 5
    fmt.Println(5 + 2 - 3) // Prints 4
    fmt.Println(2 * 5 + 1) // Prints 11
    fmt.Println(4 / 2 * 5) // Prints 10
    

  • We can also use variables:

    num1 := 5
    num2 := -1
    
    fmt.Println(2 * num1 * num2) // Prints -10
    fmt.Println(num1 + num2 - 3) // Prints 1
    fmt.Println(20 / num1 + num2) // Prints 3
    fmt.Println(num1 / 5 * num2) // Prints -1
    



Assignment
Follow the Coding Tutorial and let's practice with mathematical expressions!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check