wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a program to swap two numbers without using a third variable.

Open in App
Solution

Program:
#defining two variables
x = 5
y = 6

#printing the values before swapping

print("The values of x and y are",x,"and",y,"respectively.")

# Using 'multiple assignment' to swap the values
x,y = y,x

#printing the values after swapping
print("The values of x and y after swapping are",x,"and",y,"respectively.")


OUTPUT:
The values of x and y are 5 and 6 respectively.
The values of x and y after swapping are 6 and 5 respectively.

flag
Suggest Corrections
thumbs-up
18
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Applications (Word problem)
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon