Python Program for Swapping Three Variables Without Using any Temporary Variable.

In the previous article, we have discussed Python Program to Toggle the Last m Bits

Given three numbers and the task is to swap the given three numbers without using any temporary variable in python

Examples:

Example1:

Input:

Given first number= 30
Given second number= 45
Given third number= 21

Output:

The above given three numbers before swapping :
first number =  30 second number =  45 third number =  21
The above given three numbers after swapping without using temporary variable:
first number =  21 second number =  30 third number =  45

Example2:

Input:

Given first number= 60
Given second number= 80
Given third number= 70

Output:

The above given three numbers before swapping :
first number =  60 second number =  80 third number =  70
The above given three numbers after swapping without using temporary variable:
first number =  70 second number =  60 third number =  80

Program for Swapping Three Variables Without Using any Temporary Variable in Python

Below are the ways to swap given three numbers without using any temporary variable in python:

Method #1: Using Arithmetic Operators (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Give the third number as static input and store it in another variable.
  • Add first, second, and third numbers and assign the result to the first number.
  • Add second, and third numbers and subtract the result from the given first number.
  • Assign the result to the second number.
  • Add second, and third numbers and subtract the result from the given first number.
  • Assign the result to the given third number.
  • Add second, and third numbers and subtract the result from the given first number.
  • Assign the result to the given first number.
  • Print the given three numbers after swapping without using a temporary variable.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
fst_numb = 30
# Give the second number as static input and store it in another variable.
scnd_numb = 45
# Give the third number as static input and store it in another variable.
thrd_numb = 21
print("The above given three numbers before swapping :")
print("first number = ", fst_numb, "second number = ",
      scnd_numb, "third number = ", thrd_numb)
# Add first, second, and third numbers and assign the result to the first number.
fst_numb = fst_numb + scnd_numb + thrd_numb
# Add second, and third numbers and subtract the result from the given first number.
# Assign the result to the second number.
scnd_numb = fst_numb - (scnd_numb+thrd_numb)
# Add second, and third numbers and subtract the result from the given first number.
# Assign the result to the given third number.
thrd_numb = fst_numb - (scnd_numb+thrd_numb)
# Add second, and third numbers and subtract the result from the given first number.
# Assign the result to the given first number.
fst_numb = fst_numb - (scnd_numb+thrd_numb)
# Print the given three numbers after swapping without using a temporary variable.
print("The above given three numbers after swapping without using temporary variable:")
print("first number = ", fst_numb, "second number = ",
      scnd_numb, "third number = ", thrd_numb)

Output:

The above given three numbers before swapping :
first number =  30 second number =  45 third number =  21
The above given three numbers after swapping without using temporary variable:
first number =  21 second number =  30 third number =  45

Method #2: Using Arithmetic Operators (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Give the third number as user input using the int(input()) function and store it in another variable.
  • Add first, second, and third numbers and assign the result to the first number.
  • Add second, and third numbers and subtract the result from the given first number.
  • Assign the result to the second number.
  • Add second, and third numbers and subtract the result from the given first number.
  • Assign the result to the given third number.
  • Add second, and third numbers and subtract the result from the given first number.
  • Assign the result to the given first number.
  • Print the given three numbers after swapping without using a temporary variable.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as user input using the int(input()) function and
# store it in a variable.
fst_numb = int(input('Enter some random number = '))
# Give the second number as user input using the int(input()) function and
# store it in another variable.
scnd_numb = int(input('Enter some random number = '))
# Give the third number as user input using the int(input()) function and 
# store it in another variable.
thrd_numb = int(input('Enter some random number = '))
print("The above given three numbers before swapping :")
print("first number = ", fst_numb, "second number = ",
      scnd_numb, "third number = ", thrd_numb)
# Add first, second, and third numbers and assign the result to the first number.
fst_numb = fst_numb + scnd_numb + thrd_numb
# Add second, and third numbers and subtract the result from the given first number.
# Assign the result to the second number.
scnd_numb = fst_numb - (scnd_numb+thrd_numb)
# Add second, and third numbers and subtract the result from the given first number.
# Assign the result to the given third number.
thrd_numb = fst_numb - (scnd_numb+thrd_numb)
# Add second, and third numbers and subtract the result from the given first number.
# Assign the result to the given first number.
fst_numb = fst_numb - (scnd_numb+thrd_numb)
# Print the given three numbers after swapping without using a temporary variable.
print("The above given three numbers after swapping without using temporary variable:")
print("first number = ", fst_numb, "second number = ",
      scnd_numb, "third number = ", thrd_numb)

Output:

Enter some random number = 60
Enter some random number = 80
Enter some random number = 70
The above given three numbers before swapping :
first number = 60 second number = 80 third number = 70
The above given three numbers after swapping without using temporary variable:
first number = 70 second number = 60 third number = 80

Note: It’s worth noting that the above method causes the stack to overflow when the variables have huge values. When dealing with enormous numbers, the solution is to adopt a different strategy.

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.