Swapping numbers
Swapping numbers means exchanging the values between two or more variables. In this program, we are going to see how we can swap two user-given number with the help of one temporary variable.
Logic
- Assign variable a’s value to variable temp, so now the temp is holding a’s original value.
- Assign b’s value to a.
- Assign temp’s (holding a) value to b.
Program
# Take input from user a = int(input("Enter number a : ")) b = int(input("\nEnter number b : ")) print("\nBefore swapping a = " + str(a) + " b = " + str(b)) # Swapping number temp = a a = b b = temp print("After swapping a = " + str(a) + " b = " + str(b))
Output
Enter number a : 10
Enter number b : 200
Before swapping a = 10 b = 200
After swapping a = 200 b = 10