Friday, 11 August 2017

swapping of two numbers in python

swapping of two numbers by using temporary variable

a=int(input("enter a value: "))

b=int(input("enter b value: "))

print("before swapping: ","a=",a,"b=",b)

temp=a

a=b

b=temp

print("after swapping: ","a=",a,"b=",b)

Output:

enter a value: 4

enter b value: 5

before swapping:  a= 4 b= 5


after swapping:  a= 5 b= 4



swapping of two numbers 

a=int(input("enter a value: "))

b=int(input("enter b value: "))

print("before swapping: ","a=",a,"b=",b)

a,b=b,a

print("after swapping: ","a=",a,"b=",b)

Output:

enter a value: 4

enter b value: 5

before swapping:  a= 4 b= 5


after swapping:  a= 5 b= 4

No comments:

Post a Comment