Golang Arithmetic Operators

Here, we are going to learn about the Arithmetic Operators in the Go programming language with examples.
Submitted by IncludeHelp, on December 06, 2021

Arithmetic Operators

Arithmetic or Mathematical operators are used to perform mathematic operations such as addition, subtraction, multiplication, etc.

List of Golang Arithmetic Operators

Operator Description Example
x:=5
y:=2
Addition (+) Adds two operands x+y returns 7
Subtraction (-) Subtracts two operands (subtracts the seconds operand from the first operand) x-y returns 3
Multiplication (*) Multiplies two operands x*y returns 10
Division (/) Divides the first operand with the second operand and returns the result x/y returns 2
Modulus (%) Returns the remainder of two operands x%y returns 1
Increment (++) Increases the value of the variable by one x++ returns 6
Decrement (--) Decreases the value of the variable by one x-- returns 4

Example of Golang Arithmetic Operators

The below Golang program is demonstrating the example of arithmetic operators.

// Golang program demonstrate the
// example of arithmetic operators

package main

import "fmt"

func main() {
	x := 5
	y := 2
	result := 0

	result = x + y
	fmt.Println(x, "+", y, "=", result)

	result = x - y
	fmt.Println(x, "-", y, "=", result)

	result = x * y
	fmt.Println(x, "*", y, "=", result)

	result = x / y
	fmt.Println(x, "/", y, "=", result)

	result = x % y
	fmt.Println(x, "%", y, "=", result)

	x++
	fmt.Println("x++ =", x)

	y--
	fmt.Println("y-- =", y)
}

Output:

5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 1
x++ = 6
y-- = 1



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.