Saturday 26 December 2020

Python program to swap two variables

Python program to swap two variables

Python Code:

# Read inputs from the user
x = input('Enter value of x: ')
y = input('Enter value of y: ')

print('Value of x before swapping: ', x)
print('Value of y before swapping: ', y)

# using temporary variable to swap values
temp = x
x = y
y = temp

print('Value of x after swapping: ', x)
print('Value of y after swapping: ', y)

Output of program:

Enter value of x: 10 Enter value of y: 20 Value of x before swapping: 10 Value of y before swapping: 20 Value of x after swapping: 20 Value of y after swapping: 10



No comments:

Post a Comment