English

Write a program to swap two numbers using a third variable. - Computer Science (Python)

Advertisements
Advertisements

Question

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

Answer in Brief

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.")

# k is the third variable used to swap values between x and y
k = x                        
x = y
y = k

#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.
shaalaa.com
Variables
  Is there an error in this question or solution?
Chapter 5: Getting Started with Python - Exercise [Page 118]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 5 Getting Started with Python
Exercise | Q 14. | Page 118
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×