7.WAP to accept any two numbers and exchange their values without using the third variable.
7.WAP to accept any two numbers and exchange their values without using the third variable.
Method 1:
Code:
a=int(input("Enter the 1 number "))
b=int(input("Enter the 2 number "))
print("Before swapping ")
print("a=",a," b=",b)
a=a+b
b=a-b
a=a-b
print("Before swapping ")
print("a=",a," b=",b)
Output:
Enter the 1 number 5
Enter the 2 number 6
Before swapping
a= 6 b= 3
Before swapping
a= 3 b= 6
Method 2:
Code:
a=int(input("Enter the 1 number "))
b=int(input("Enter the 2 number "))
print("Before swapping ")
print("a=",a," b=",b)
a,b=b,a
print("Before swapping ")
print("a=",a," b=",b)
Output:
Enter the 1 number 5
Enter the 2 number 6
Before swapping
a= 6 b= 3
Before swapping
a= 3 b= 6
Comments
Post a Comment