Table of Contents
Program to Swap Two Numbers
Method 1
Method 2
Method 3
Method 4
Method 5
Method 1
# Python program to swap two Number
a = input('Enter value of a: ')
b = input('Enter value of b: ')
t= a
a =b
b = t
print("The value a after swapping: ",a)
print("The value b after swapping: ",b)
Method 2
# Python program to swap two variables
a = input('Enter value of a: ')
b = input('Enter value of b: ')
a, b = b, a
print("The value a after swapping: ",a)
print("The value b after swapping: ",b)
Method 3
# Python program to swap two Numbers using Addition and Subtraction
a = int(input('Enter value of a: '))
b = int(input('Enter value of b: '))
a = a + b
b = a - b
a = a - b
print("The value a after swapping: ",a)
print("The value b after swapping: ",b)
Method 4
# Python program to swap two Numbers using Multiplication and Division
a = int(input('Enter value of a: '))
b = int(input('Enter value of b: '))
a = a * b
b = a / b
a = a / b
print("The value a after swapping: ",a)
print("The value b after swapping: ",b)
Method 5
# Python program to swap two Numbers using XOR swap
a = int(input('Enter value of a: '))
b = int(input('Enter value of b: '))
a = a ^ b
b = a ^ b
a = a ^ b
print("The value a after swapping: ",a)
print("The value b after swapping: ",b)