Signup/Sign In

Python program to swap two numbers without using third variable

learn Python language tutorial

In this article, you will learn to write a python program that will swap the numbers without introducing any new variable. Here, we will use two different approaches to do so. In the first method, we will simply exchange the values and assign them to the two variables. In the second method, we will use some of the arithmetic operations like addition and subtraction to get the output as swapped numbers.

Look at the given example to understand the working with input and output. We are representing the first number by num1 and the second number by num2.

Input:

num1=23

num2=44

Output:

num1=44

num2=23

Let us take one more example to understand clearly,

Input:

num1=80

num2=3

Output:

num1=3

num2=80

Python Program 1:- Directly using the inbuilt method.

Before moving towards the program, it is important to know that we can assign the value of two variables together in python using a shortcut like a,b=5,3.

Here, the values are given to the variables sequentially the value of "a" is equal to 5 and that of "b" is equal to 3.

Algorithm

Step 1: Take the user inputted value for the two numbers.

Step 2: Now we can directly assign the values for two numbers as

Look at the complete program given below to understand the implementation of the approach.

#program to swap two numbers without using third variable

#Input
num1 = input('Enter the value of first number (num1): ')
num2 = input('Enter the value of second number (num2): ')

#swap the values
num1,num2=num2,num1

#Output
print('The value of num1 after swapping: {}'.format(num1))
print('The value of num2 after swapping: {}'.format(num2))


Enter the value of first number (num1): 2
Enter the value of second number (num2): 5
The value of num1 after swapping: 5
The value of num2 after swapping: 2

Python Program 2:- Using addition and subtraction operators to exchange the values.

Here, the values of the given numbers are exchanged using the addition and subtraction operators simultaneously.

Algorithm

Step 1: Take the input value for the two numbers by the user.

Step 2: Assign num1=num1+num2.

Step 3: Assign num2=num1-num2.

Step 4: Finally the value of num2 can be written as: num1=num1-num2.

Look at the complete program given below to understand the implementation of the approach.

#program to swap two numbers without using third variable

#Input
num1 = input('Enter the value of first number (num1): ')
num2 = input('Enter the value of second number (num2): ')

#swap the values
num1 = num1+num2
num2 = num1-num2
num1 = num1-num2

#Output
print('The value of num1 after swapping: {}'.format(num1))
print('The value of num2 after swapping: {}'.format(num2))


Enter the value of first number (num1): 98
Enter the value of second number (num2): 15
The value of num1 after swapping: 15
The value of num2 after swapping: 98

Python Program 3:- Using bitwise XOR swapping.

Here, we will use one of the bitwise operator i.e. XOR. This method only works for integers and works faster because this method uses bit operation (for same values, output = 0 and for different values, output = 1).

Algorithm

Step 1: Take the input value for the two numbers by the user.

Step 2: Assign num1=num1^num2.

Step 3: Assign num2=num2^num1.

Step 4: Finally the value of num2 can be written as: num1=num1^num2.

Look at the complete program given below to understand the implementation of the approach.

#program to swap two numbers using third variable

#Input
num1 = input('Enter value of first number (num1): ')
num2 = input('Enter value of second number (num2): ')

#XOR operator to swap the values
num1 = num1^num2
num2 = num2^num1
num1 = num1^num2

#Output
print('The value of num1 after swapping: {}'.format(num1))
print('The value of num2 after swapping: {}'.format(num2))


Enter the value of first number (num1): 5
Enter the value of second number (num2): 10
The value of num1 after swapping: 10
The value of num2 after swapping: 5

Python Program 4:- Using the division and multiplication operator for swapping.

Here, the values of the given numbers are exchanged using the division and multiplication operators simultaneously. Since the division by zero is undefined so we can not use 0 as a numeric value here.

Algorithm

Step 1: Take the input value for the two numbers by the user.

Step 2: Assign num1=num1*num2.

Step 3: Assign num2=num1/num2.

Step 4: Finally the value of num2 can be written as: num1=num1/num2.

Look at the complete program given below to understand the implementation of the approach.

#program to swap two numbers using third variable

#Input
num1 = input('Enter value of first number (num1): ')
num2 = input('Enter value of second number (num2): ')

#XOR operator to swap the values
num1 = num1*num2
num2 = num1/num2
num1 = num1/num2

#Output
print('The value of num1 after swapping: {}'.format(num1))
print('The value of num2 after swapping: {}'.format(num2))


Enter the value of first number (num1): 2
Enter the value of second number (num2): 11
The value of num1 after swapping: 11
The value of num2 after swapping: 2

Conclusion

We have learned four different methods by which we can swap two numbers without using any third variable in Python. The first two of these methods apply to all inbuilt datatypes like float, int, char, etc.... where the bitwise method only works for integers and the fourth method is applicable for all numeric values except 0.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.