There are many kinds of approaches or systems to Swap two varribles :)
As we know that a problem can be sloved in different ways, right?
Basically, we will see three different approaches to Swap two Varribles
Those are:
- Using Temp (Legit Approach)
- Mathmatical Apporach
- Using Systematical Varrible
Using Temp Var
a = 7
b = 4
print(a, b)
temp = a
a = b
b = temp
print(a, b) # and that's all, varrible has been swapped
Mathmatical Apporach
a = 9
b = 4 # remember it's only applicable
# when both varribles are numeric
# like (int, float)
a = (a + b)
b = a - b
a = a - b
# and it's done !
Using Using Systematical Varrible
a = 12
b = 5
a, b = b, a
# and doen, but sounds wired :(
So, that's all, but the first Apporach is legit
0 Comments