Swift Multiplication Operator
To perform multiplication of two numbers in Swift, we can use Swift Multiplication Arithmetic operator.
* takes two numbers as operands and returns the product of the two operands.
The syntax of Multiplication Operator is
a * b
b are left and right operands respectively.
These operands should be of same type. For example,
bcan be Int.
bcan be Float.
bcan be Double, etc.
1. Multiplication of integer values
In the following program, we will perform multiplication of two integers.
var a = 3 var b = 4 var result = a * b print("The result is \(result)")
2. Multiplication of Double values
Now, let us find the multiplication of two Double values.
var a = 3.2 var b = 4.1 var result = a * b print("The result is \(result)")
3. Multiplication of values that belong to different datatypes
If we try to multiply numbers of different types, we would get Swift Compiler Error as shown below.
a is Double and
b is Int, Multiplication
- operator raises Compiler Error.
In this Swift Tutorial, we learned how to perform Multiplication in Swift programming using Multiplication Operator. We have seen the syntax and usage of Multiplication Operator with examples.