This is a Python Program to exchange the values of two numbers without using a temporary variable.
The program takes both the values from the user and swaps them without using temporary variable.
Here is source code of the Python Program to exchange the values of two numbers without using a temporary variable. The program output is also shown below.
1 2 3 4 5 6 7 8 |
a=int(input("Enter value of first variable: ")) b=int(input("Enter value of second variable: ")) a=a+b b=a-b a=a-b print("a is:",a," b is:",b) |
1. User must first enter the values for both the elements.
2. The first element is assigned the sum of the first two elements.
3. Second element is assigned the difference between the sum in the first variable and the second variable, which is basically the first element.
4. Later the first element is assigned the difference between the sum in the variable and the second variable, which is the second element.
5. Then the swapped values are printed.