Questions
Write a program to exchanging values of two variables.
Last Updated : 23 May 2026
Jan 19, Jul 22
Solution:
# Get input from the user
a = int(input("Enter First number: "))
b = int(input("Enter Second number: "))
# Show values before swapping
print("Before swapping a = ", a, "b = ", b)
# Swap the values
a, b = b, a
# Show values after swapping
print("After swapping a = ", a, "b = ", b)
OUTPUT:
Enter First number15
Enter Second number24
Before swaping a= 15 b= 24
After swaping a= 24 b= 15
Explanation:
-
Input:
aandbare read and converted to integers.
-
Before Swapping:
- Prints
aandb.
- Prints
-
Swap:
a, b = b, aexchanges the values.
-
After Swapping:
- Prints the swapped values of
aandb.
- Prints the swapped values of
Meanwhile you can watch this video
Watch Video