In this tutorial, we will go through Swift programs to swap two numbers.
Swift Program to Swap Numbers using Tuple Syntax
In the following program, we will use tuple syntax to swap integers in two variables.
main.swift
var a = 10 var b = 5 print("Before swapping: a = \(a), b = \(b)") (a, b) = (b, a) print("After swapping: a = \(a), b = \(b)")
Output
Swift Program to Swap Numbers using Arithmetic Operators
In the following program, we will use Addition and Subtraction Operators to swap integers in two variables.
main.swift
var a = 10 var b = 5 print("Before swapping: a = \(a), b = \(b)") a = a + b b = a - b a = a - b print("After swapping: a = \(a), b = \(b)")
Output
Conclusion
In this Swift Tutorial, we learned how to find the factorial of a given number using for-in statement, recursion, etc., with examples.