Swift - Arithmetic Operators



Arithmetic Operators in Swift

Operators are the special symbols provided by the Swift that tell the compiler to perform a specific operation. Swift supports various operators. Among all of these operators arithmetic operators are the most commonly used operators.

As the name suggested arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, etc with all numeric data types like integers, float, etc. In Swift, arithmetic operators do not allow their values to overflow by default, if you want this kind of behaviour, then use overflow operators.

Swift supports the following arithmetic operators −

Operator Name Example
+ Addition 20 + 30 = 50
- Subtraction 30 - 4 = 26
* Multiplication 3 * 4 = 12
/ Division 12 / 6 = 2
% Remainder or Modulus 12 % 2 = 0

Addition Operator in Swift

The addition operator is used to add the value of two variables. Or we can say that it is used to add two operands of any data type(such as integer, float, etc.). It is also used to concatenate two or more strings into a single string.

Syntax

Following is the syntax of the addition operator −

var result = value1 + value2

Example

Swift program to perform addition between numeric variables using addition operator (+).

import Foundation

// Double data type
let num1 = 45.3
let num2 = 23.5

// Using addition operator
var result1 = num1 + num2

print("Sum of \(num1) and \(num2) is \(result1)")

// Integer data type
let num3 = 45
let num4 = 12

// Using addition operator
var result2 = num3 + num4

print("Sum of \(num3) and \(num4) is \(result2)")

Output

Sum of 45.3 and 23.5 is 68.8
Sum of 45 and 12 is 57

Example

Swift program to perform concatenation using addition operator (+).

import Foundation

let str1 = "Swift"
let str2 = "Programming"

// Concatenating two strings
// Using addition operator
var result = str1 + str2
print(result)

Output

SwiftProgramming

Subtraction Operator in Swift

Subtraction operation is used to subtract the value of one variable from another. Or we can say that it is used to perform subtraction between two operands.

Syntax

Following is the syntax of the subtraction operator −

var result = value1 - value2

Example

Swift program to perform a subtraction between variables using subtract operator "-".

import Foundation

// Double data type
let num1 = 25.8
let num2 = 12.4

// Using the subtraction operator
var result1 = num1 - num2

print("Subtract \(num1) from \(num2) = \(result1)")

// Integer data type
let num3 = 26
let num4 = 17

// Using the subtraction operator
var result2 = num3 - num4

print("Subtract \(num3) from \(num4) = \(result2)")

Output

Subtract 25.8 from 12.4 = 13.4
Subtract 26 from 17 = 9

Division Operator in Swift

A division operator is used to divide the value of the first variable from another. In other words, the division operator is used to perform division between two operands. This operator works only with numeric values.

Syntax

Following is the syntax of the division operator −

var result = value1 / value2

Example

Swift program to perform a division between variables using division operator (/).

import Foundation

// Double data type
let num1 = 34.5
let num2 = 3.2

// Using division operator
var result1 = num1 / num2

print("Divide \(num1) by \(num2) = \(result1)")

// Integer data type
let num3 = 14
let num4 = 7

// Using division operator
var result2 = num3 / num4

print("Divide \(num3) by \(num4) = \(result2)")

Output

Divide 34.5 by 3.2 = 10.78125
Divide 14 by 7 = 2

Multiplication Operator in Swift

A multiplication operator is used to multiply a numeric variable by another numeric variable. In other words, the multiplication operator is used to perform multiplication between two operands.

Syntax

Following is the syntax of the division operator −

var result = value1 * value2

Example

Swift program to perform a multiplication between variables using multiply operator (*).

import Foundation

// Double data type
let num1 = 34.5
let num2 = 3.2

// Using the multiplication operator
var result1 = num1 * num2

print("Multiply \(num1) by \(num2) = \(result1)")

// Integer data type
let num3 = 14
let num4 = 2

// Using multiplication operator
var result2 = num3 * num4

print("Multiply \(num3) by \(num4) = \(result2)")

Output

Multiply 34.5 by 3.2 = 110.4
Multiply 14 by 2 = 28

Remainder Operator in Swift

The remainder operator is used to find the remainder left after dividing the values of two numeric variables. It is also known as a modulo operator. It always ignores the negative sign of the second variable or operand, which means the result of x % y and x % -y is always the same. Whereas the result of -x % y and x % y is always different.

Syntax

Following is the syntax of the remainder operator −

var result = value1  % value2

Example

Swift program to calculate remainder between variables using remainder operator (%).

import Foundation

// Double data type
let num1 = -18 
let num2 = 7

// Finding remainder
var result1 = num1 % num2

print("Remainder is \(result1)")

// Integer data type
let num3 = 2341
let num4 = -2

// Finding remainder
var result2 = num3 % num4

print("Remainder is \(result2)")

Output

Remainder is -4
Remainder is 1
swift_operators.htm
Advertisements