: : Easy python program for swapping the two variables. allinformx.blogspot.com

Easy python program for swapping the two variables. allinformx.blogspot.com

In this example we will learn to swap two variable using temporary variable and, without temporary variable.

Easy python program for swapping the two variables

1.Using arithmetic operation.
print("hello")
#the program that swap the value that you will enter.
x=int(input("enter the value of x:"))
y=int(input("enter the value of y:"))
x=x+y
y=x-y
x=x-y
print("after the swapping the value ")
print("the value of x is","=",x)
print("the value of y is","=",y)
print("Thank you")

Output:
hello
enter the value of x:5
enter the value of y:6
after the swapping the value
the value of x is=6
the value of y is=5
Thank you


    Output
In this example for swapping the entered number we add the value of x and y and stored it to x and then subtract the the value of y from x and stored it to the y. and also stored it to x. 
2.without using a temporary variable

Print("hello")

# the program that swap the values that you will enterd. 
print("For swap please enter the number")
x=int(input("enter a value of x:"))
y=int(input("enter a value of y:"))
x,y=y,x
print("After a swapping")
print("The value of x","=",x)
print("The value of y","=",y)
print("Thank you")

Output
hello
For swap please enter the number
X=enter a value of x:5
Y=enter a value of y:6
The value of x=6
The value of y=5
Thank you


Output
In this example program we have only stored the value of x into y and y into the x. 

3.using temporary variable

print("hello")
# the program that swap the values that you will enter.
print("For swap please enter the number")
x=int(input("enter a value of x:"))
y=int(input("enter a value of y:"))
temp=x
x=y
y=temp
print("After a swapping")
print("The value of x","=",x)
print("The value of y","=",y)

Output
hello
For swap please enter a number
enter a value of x:5
enter a value of y:6
After a swapping
The value of x=6
The value of y=5


Output

In this program, we use the temp variable to hold the value of x temporarily. We then put the value of y in x and later temp in y. In this way, the values get exchanged.

4.using XOR swap
This algorithm works for integer only
print("hello")
#the program that swap the value that you will enter.
x=int(input("enter the value of x:"))
y=int(input("enter the value of y:"))
x=x^y
y=x^y
x=x^y
print("after the swapping the value ")
print("the value of x is","=",x)
print("the value of y is","=",y)
print("Thank you")

Output
hello
enter the value of x:5
enter the value of y:6
After the swapping the value
the value of x is=6
the value of y is=5
Thank you


Output

5.using multiplication and division
print("hello")
#the program that swap the value that you will enter.
x=int(input("enter the value of x:"))
y=int(input("enter the value of y:"))
x=x*y
y=x/y
x=x/y
print("after the swapping the value ")
print("the value of x is","=",x)
print("the value of y is","=",y)
print("Thank you")

Output
hello
enter the value of x:5
enter the value of y:6
after the swapping the value
the value of x is=6
the value of y is=5
Thank you


Output



Visit blogs:


Post a Comment

0 Comments