Arithmetic operators help you do math with numbers in your program. They let you add, subtract, multiply, and divide values easily.
Arithmetic operators in Go
result := a + b // addition result := a - b // subtraction result := a * b // multiplication result := a / b // division result := a % b // remainder (modulus)
Use + to add, - to subtract, * to multiply, / to divide, and % to get the remainder.
Division between integers gives an integer result (no decimals). Use float types for decimal division.
sum := 5 + 3 fmt.Println(sum) // prints 8
diff := 10 - 4 fmt.Println(diff) // prints 6
product := 7 * 6 fmt.Println(product) // prints 42
quotient := 20 / 3 fmt.Println(quotient) // prints 6 remainder := 20 % 3 fmt.Println(remainder) // prints 2
This program shows how to use all basic arithmetic operators with two numbers and prints the results.
package main import "fmt" func main() { a := 15 b := 4 sum := a + b diff := a - b product := a * b quotient := a / b remainder := a % b fmt.Println("Sum:", sum) fmt.Println("Difference:", diff) fmt.Println("Product:", product) fmt.Println("Quotient:", quotient) fmt.Println("Remainder:", remainder) }
Remember that dividing two integers results in an integer. To get a decimal result, convert numbers to float64 first.
The modulus operator % only works with integers in Go.
Arithmetic operators let you do basic math in Go.
Use +, -, *, /, and % for addition, subtraction, multiplication, division, and remainder.
Integer division drops decimals; use floats for precise division.
Practice
Solution
Step 1: Understand the remainder operation
The remainder operator returns what is left after division.Step 2: Identify the correct operator in Go
In Go, the remainder operator is represented by the percent sign '%'.Final Answer:
% -> Option DQuick Check:
Remainder = % [OK]
- Confusing division / with remainder %
- Using * for remainder
- Using + for remainder
Solution
Step 1: Recall Go's multiplication syntax
Go uses the asterisk '*' symbol for multiplication.Step 2: Check each option
Only 'a * b' uses the correct multiplication operator.Final Answer:
a * b -> Option CQuick Check:
Multiplication = * [OK]
- Using 'x' instead of '*'
- Using '.' or '&' for multiplication
- Confusing syntax with other languages
package main
import "fmt"
func main() {
a := 7
b := 3
fmt.Println(a / b)
}Solution
Step 1: Identify variable types and operation
Both a and b are integers, so division is integer division.Step 2: Calculate integer division result
7 divided by 3 is 2.333..., but integer division drops decimals, so result is 2.Final Answer:
2 -> Option BQuick Check:
Integer division 7/3 = 2 [OK]
- Expecting float result
- Confusing integer division with float division
- Using wrong operator
package main
import "fmt"
func main() {
x := 10
y := 0
fmt.Println(x / y)
}Solution
Step 1: Analyze the division operation
The code divides x by y, where y is zero.Step 2: Understand Go's behavior on division by zero
Dividing by zero causes a runtime panic (error) in Go.Final Answer:
Division by zero causes runtime panic -> Option AQuick Check:
Division by zero = runtime panic [OK]
- Thinking it prints 0
- Expecting compile-time error
- Ignoring runtime panic
package main
import "fmt"
func main() {
a := 15
b := 4
c := a % b
d := a / b
fmt.Println(c + d)
}Solution
Step 1: Calculate remainder c = a % b
15 % 4 equals 3 because 4*3=12 and 15-12=3.Step 2: Calculate integer division d = a / b
15 / 4 equals 3 because integer division drops decimals.Step 3: Add c + d
3 + 3 equals 6.Final Answer:
6 -> Option AQuick Check:
15 % 4 = 3, 15 / 4 = 3, 3 + 3 = 6 [OK]
- Miscomputing remainder
- Using float division instead of integer
- Adding wrong values
Ready to prove it?
Pass the test and this pattern moves unknown
