Write a python script to enter two numbers a, b, then swap (interchange) the
value of these two Variable using third variable temp.
Example: If A=12 and B=23, then after swapping value of A=23 and B=12.
with output
Answers
Answered by
7
Required Answer:-
Question:
- Write a python script to enter two numbers and interchange their values using third variable temp.
Solution:
Here is the code.
a=int(input("Enter a: "))
b=int(input("Enter b: "))
print("Before swapping,")
print("a: ",a)
print("b: ",b)
temp=a
a=b
b=temp
print("After swapping,")
print("a: ",a)
print("b: ",b)
Dry run:
In this approach, we have created three variables for swapping i.e, a, b and c.
Consider a = 2 and b = 3
So,
temp = a = 2
a = b = 3
b = temp = 2
We will store the value of a in variable temp, then store the value of b in a. Now, the value of a is erased from that location. To get back, we have created a temporary variable (temp) to store the value of a. Now, assign b to temp.
Output is attached.
Attachments:
Answered by
0
Answer:
hope it helps you to....
Attachments:
Similar questions