divide two numbers in swift

To divide two numbers in Swift, you can use the division operator /. Here's an example:

main.swift
let quotient = 10 / 2
print(quotient) // Output: 5
51 chars
3 lines

In the above example, we're dividing the number 10 by 2, which results in the quotient 5. Note that the result of this operation is automatically inferred by Swift to be of type Int.

If you want to perform division on two floating-point numbers, you can use the same division operator /. Here's an example:

main.swift
let quotient = 10.0 / 3.0
print(quotient) // Output: 3.3333333333333335
72 chars
3 lines

In the above example, we're dividing the number 10.0 by 3.0, which results in the quotient 3.3333333333333335. Note that the result of this operation is automatically inferred by Swift to be of type Double.

gistlibby LogSnag