The isMultiple()
method checks if one number is multiple of another or not.
Example
// check is 9 is multiple of 3 or not
var result = 9.isMultiple(of: 3)
print(result)
// Output: true
isMultiple() Syntax
The syntax of the isMultiple()
method is:
num.isMultiple(of: otherNumber)
Here, num
is a number.
isMultiple() Parameters
The isMultiple()
method takes one parameter
otherNumber
- the value to test
isMultiple() Return Values
The isMultiple()
method returns boolean value
true
- ifnum
is multiple ofotherNumber
false
- ifnum
is not a multiple ofotherNumber
Example 1: Swift Double isMultiple()
// check if 4 is multiple of 2 or not
var result1 = 4.isMultiple(of: 2)
print(result1)
// check if 2 is multiple of 4 or not
var result2 = 2.isMultiple(of: 4)
print(result2)
// check if 210 is multiple of 10 or not
var result3 = 210.isMultiple(of: 10)
print(result3)
Output
true false true
Here, since 4 is a multiple of 2, the isMultiple()
method returns true
. However, 2 is not a multiple of 4, the method returns false
.
Lastly, since 210 is multiple of 10, the method returns true
.
Example 2: Check Odd or Even
// check if 48 is multiple of 2 or not
if 48.isMultiple(of: 2) {
print("Even Number")
}
else {
print("Odd number")
}
Output
Even Number
Here, since 48 is multiple of 2, the code inside the if
block is executed.