Python Program to find swap two numbers without temp variable (Addition and Subtraction Method)

Program

a = int(input("Enter the value of a:\t"))
b = int(input("Enter the value of b:\t"))
print("Values before swapping:")
print("a:\t{}\nb:\t{}".format(a,b))
a = a + b
b = a - b
a = a - b
print("Values after swapping:")
print("a:\t{}\nb:\t{}".format(a,b))

Output

Enter the value of a:	12
Enter the value of b:	3
Values before swapping:
a:	12
b:	3
Values after swapping:
a:	3
b:	12

Tags: