# Swap Numbers without using temporary variable in Swift Program?

This tutorial will discuss how to write a swift program to swap two numbers without using temporary variable. Swapping of two variables means mutually exchanging the values of two variables.

## Swapping two numbers without using temporary variables

Given two variables Number1 and Number2 now we swap their values without using any temporary variable. Here we use tuple to preform the swapping operation.

### Algorithm

The algorithm is explained below −

• Step 1 − Declare two integer variables − Number1, and Number2

• Step 2 − Assign values to Number1 and Number2

• Step 3 − Use tuple syntax to swap −

(Number1, Number2) = (Number2, Number1)

• Step 4 − Display the value of Number1 and Number2 variables

### Example

The following Swift program will show how to swap two numbers without using temporary variable.

import Foundation
import Glibc

var Number1 = 32
var Number2 = 54

print("Number 1 before swapping:", Number1)
print("Number 2 before swapping:", Number2)

(Number1, Number2) = (Number2, Number1)

print("\nNumber 1 after swapping:", Number1)
print("Number 2 after swapping:", Number2)

### Output

Number 1 before swapping: 32
Number 2 before swapping: 54

Number 1 after swapping: 54
Number 2 after swapping: 32

In the above code, we have two numbers that are Number1 = 32 and Number2 = 54 now we swap them using tuple. So the values after swapping are Number1 = 54 and Number2 = 32.

## Swapping number using addition and subtraction operation

Given two variables Num_1 and Num_2 now we swap their values using addition and subtraction operation.

### Algorithm

The algorithm is explained below −

• Step 1 − Declare two integer variables − Num_1, and Num_2

• Step 2 − Assign values to Num_1 and Num_2

• Step 3 − Swap numbers using addition and subtraction operation −

Num_1 = Num_1 + Num_2

Num_2 = Num_1 - Num_2

Num_1 = Num_1 - Num_2

• Step 4 − Display the value of Num_1 and Num_2 variables

• Step 5 − End

### Example

The following Swift program will show how to swap two numbers using addition and subtraction operation.

import Foundation
import Glibc

var Number1 = 30
var Number2 = 59

print("Number 1 before swapping:", Number1)
print("Number 2 before swapping:", Number2)

Number1 = Number1 + Number2
Number2 = Number1 - Number2
Number1 = Number1 - Number2

print("\nNumber 1 after swapping:", Number1)
print("Number 2 after swapping:", Number2)

### Output

Number 1 before swapping: 30
Number 2 before swapping: 59

Number 1 after swapping: 59
Number 2 after swapping: 30

In the above code, we have two numbers that are Number1 = 30 and Number2 = 59 now we swap them using addition and subtraction operation. The following code will show the swapping −

Number1 = Number1 + Number2 // Number1i.e.30 + 59 = 89

Number2 = Number1 - Number2 // Number2 i.e.,89 - 59 = 30

Number1 = Number1 - Number2 // Number1i.e. 89 - 30 = 59

So the values after swapping are Number1 = 59 and Number2 = 30.

## Swapping numbers using multiplication and division operation

Given two variables Number1 and Number2 now we swap their values using multiplication and division operation. But this method have one draw back that is if the value of any one variable is zero this this will give error.

### Algorithm

The algorithm is explained below −

• Step 1 − Start

• Step 2 − Declare two integer variables: Number1, and Number2

• Step 3 − Assign values to Number1 and Number2

• Step 4 − Swap numbers using multiplication and division operation −

Num_1 = Number1 * Number2

Num_2 = Number1 / Number2

Num_1 = Number1 / Number2

• Step 5 − Display the value of Number1 and Number2 variables

• Step 6 − End

### Example

The following Swift program will show how to swap two numbers using multiplication and division operation.

import Foundation
import Glibc

var Number1 = 38
var Number2 = 93

print("Number 1 before swapping:", Number1)
print("Number 2 before swapping:", Number2)

Number1 = Number1 * Number2
Number2 = Number1 / Number2
Number1 = Number1 / Number2

print("\nNumber 1 after swapping:", Number1)
print("Number 2 after swapping:", Number2)

### Output

Number 1 before swapping: 38
Number 2 before swapping: 93

Number 1 after swapping: 93
Number 2 after swapping: 38

In the above code, we have two numbers that are Number1 = 38 and Number2 = 93 now we swap them using multiplication and division operation. The following code will show the swapping −

Number1 = Number1 * Number2 // Number1 become 3534 i.e.38 * 93 = 3534

Number2 = Number1 / Number2 // Number2 become 38 i.e.,3534 / 93 = 38

Number1 = Number1 / Number2 // Number1 become 93 i.e.3534 / 38 = 93

So the values after swapping are Number1 = 93 and Number2 = 38.

## Swapping numbers using bitwise XOR operator

Given two variables Number1 and Number2 now we swap their values using bitwise XOR operator(^). This operator performs XOR of two numbers and return a number that has all the bits as 1 whenever the bits of two numbers differ with each other. For example, the XOR of 8(1000 in binary) and 3(0011 in binary) is 11(1011 in binary).

### Algorithm

The algorithm is explained below −

• Step 1 − Declare two integer variables: Number1, and Numb er2

• Step 2 − Assign values to Number1 and Number2

• Step 3 − Swap numbers using bitwise XOR operator

Number1 = Number1 ^ Number2

Number2 = Number1 ^ Number2

Number1 = Number1 ^ Number2

• Step 4 − Display the value of Number1 and Number2 variables

### Example

The following Swift program will show how to swap numbers using bitwise XOR operator.

import Foundation
import Glibc

var Number1 = 7
var Number2 = 4

print("Number 1 before swapping:", Number1)
print("Number 2 before swapping:", Number2)

Number1 = Number1 ^ Number2
Number2 = Number1 ^ Number2
Number1 = Number1 ^ Number2

print("\nNumber 1 after swapping:", Number1)
print("Number 2 after swapping:", Number2)

### Output

Number 1 before swapping: 7
Number 2 before swapping: 4

Number 1 after swapping: 4
Number 2 after swapping: 7

In the above code, we have two numbers that are Number1 = 7 and Number2 = 4 now we swap them using bitwise XOR operator. The following code will show the swapping −

// The binary form of 7 is 0111 and binary form of 4 is 0100

Number1 = Number1 ^ Number2 // Number1 will be 3 (binary form is 0011), i.e., 7 ^ 4 = 3

Number2 = Number1 ^ Number2 // Number2 will be 7 (binary form is 0111), i.e., 3 ^ 4 = 7

Number1 = Number1 ^ Number2 // Number2 will be 4 (binary form is 0100), i.e., 3 ^ 7 = 4

So the values after swapping are Number1 = 4 and Number2 = 7.