Swift Modulus Division

To perform modulus division of two numbers in Swift, we can use Swift Modulus Division Arithmetic Operator.

Modulus Division Operator % takes two integers as operands and returns the reminder value when the first operand is divided by the second operand.

Modulus Division Operator cannot be applied for Float/Double values.

Syntax

The syntax to find the modulus division of two numbers is

a % b

where a and b are operands. These operands should be of type Int.

ADVERTISEMENT

Examples

1. Modular Division with integer values

In the following program, we will perform modulus division of two integers.

main.swift

var a = 11
var b = 4
var result = a % b
print("The result is \(result)")

Output

Swift Modulus Division - Example

2. Modular Division with Double values

Now, let us try to find the modulus division of two Double values. We know that we cannot perform modulus division on Double or Float values.

main.swift

Swift Modulus Division - Error

Conclusion

In this Swift Tutorial, we learned how to perform Modulus Division in Swift programming using Modulus Operator. We have seen the syntax and usage of Modulus Operator with examples.