Write a program to input two numbers and inter change their values in python IDLE.

Number swapping in python can be done in multiple ways. For example, using a third or temporary variable, using arithmetic operators etc.

 In this tutorial, we will use the third or temporary variable.

 Make two variable and one temporary variable to take input two numbers from the user and store one number in the temporary variable. 

 For example: first number, second number and temporary, or simple a, b and temp. 

 

Code:

a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))

print("Before swapping the two numbers first:{0} and second:{1}".format(a,b))

temp= a
a= b
b= temp
print("After swapping the two numbers frist:{0} and second:{1}".format( a, b))

Output:

Enter the first number:15
Enter the second number:20
Before swapping the two numbers first:15 and second:20
After swapping the two numbers frist:20 and second:15 


Comments

Popular posts from this blog

Write a python program to check if it is an odd or even number using if condition.