Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
This is a Python Program to exchange the values of two numbers without using a temporary variable.
Problem Description
The program takes both the values from the user and swaps them without using temporary variable.
Problem Solution
1. Take the values of both the elements from the user.
2. Store the values in separate variables.
3. Add both the variables and store it in the first variable.
4. Subtract the second variable from the first and store it in the second variable.
5. Then, subtract the first variable from the second variable and store it in the first variable.
6. Print the swapped values.
7. Exit.
Program/Source Code
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.
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)
Runtime Test Cases
Case 1
Enter value of first variable: 3
Enter value of second variable: 5
a is: 5 b is: 3
Case 2
Enter value of first variable: 56
Enter value of second variable: 25
a is: 25 b is: 56