Wednesday, June 26, 2019

Python Program | Exchange the Values of Two Numbers Without Using a Temporary Variable

Problem 

The program takes 2 values from the user and then swaps them without any temporary variable.

Program/Source Code

Here is source code of the Python Program to exchange the values of two numbers without using a temporary variable.  
x=int(input("Enter value of 1st variable: "))
y=int(input("Enter value of 2nd variable: "))
x=x+y
y=x-y
x=x-y
print("x is:",x," y is:",y)

Runtime Test Cases

 
Test 1
Enter value of 1st variable: 10
Enter value of 2nd variable: 55
x is: 55  y is: 10
 
Test 2
Enter value of 1st variable: 101
Enter value of 2nd variable: 75
x is: 75  y is: 101
If You Enjoyed This, Take 5 Seconds To Share It