write a python programto swap two numbers...
Share
write a python programto swap two numbers...
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
# Python program to swap two variables
x = 5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
Python program and the output for the above question is listed below:
Output:
If the user input as 4 and 5, then the output is 5 and 4.
If the user input as 6 and 7, then the output is 7 and 6.
Explanation:
first_number =int(input("Enter the first number for swapping: "))#Take the first number.
Second_number =int(input("Enter the Second number for swapping: "))#Take the second number.
#Swap the number.
Third_number=first_number
first_number=Second_number
Second_number=Third_number
print("The first number and the second number after swapping is:",first_number,"and ",Second_number)#Print the number after swapping.
Code Explanation :
Learn More :