The program is written here to swap the values of two numbers without using a third variable. The process of swapping two values without using a third variable is shown below with an example.
Let a = 5 and b = 6 and the values of a and b are to be swapped.
a = a + b = 5 + 6 = 11
b = a - b = 11 - 6 = 5
a = a - b = 11 – 5 = 6
Therefore, after this a = 6 and b = 5.
INPUT: Two Numbers
OUTPUT: The numbers after exchanging the values
PROCESS:
Step 1: [Taking the inputs]
Read a, b [two numbers]
Step 2: [Swapping the two values]
Set a<-a+b
Set b<-a-b
Set a<-a-b
Print "After swapping the values are a and b"
Step 3: Stop.
TIME COMPLEXITY:
The time complexity of this program is O(1) as it requires a constant time to execute for any given input.
SPACE COMPLEXITY:
The space complexity of this program is O(1) as it requires a fixed number of memory spaces to execute the program.
Related