You can swap two numbers using a temporary variable or by using arithmetic and bitwise operations. In this article, you’ll learn about a variety of methods that enable you to swap two numbers.
How to Swap Two Numbers Using a Temporary Variable
Using a temporary variable is the simplest way to swap two numbers. Follow these three simple steps:
Step 1: Assign the value of the 1st variable to a temporary variable.
Step 2: Assign the value of the 2nd variable to the 1st variable.
Step 3: Assign the value of the temporary variable to the 2nd variable.
For example:
Let num1 = 80 and num2 = 50 (before swapping).
After step 1: num1 = 80, num2 = 50, and temp = 80.
After step 2: num1 = 50, num2 = 50, and temp = 80.
After step 3: num1 = 50, num2 = 80, and temp = 80.
Thus, num1 is equal to 50 and num2 is equal to 80 after swapping.
C++ Implementation to Swap Two Numbers Using a Temporary Variable
Below is the C++ implementation to swap two numbers using a temporary variable:
Output:
Python Implementation to Swap Two Numbers Using a Temporary Variable
Below is the Python implementation to swap two numbers using a temporary variable:
Output:
JavaScript Implementation to Swap Two Numbers Using a Temporary Variable
Below is the JavaScript implementation to swap two numbers using a temporary variable:
Output:
How to Swap Two Numbers Using Arithmetic Operators (Addition and Subtraction)
First, get the sum of two numbers. Then you can get the required numbers using the sum and subtraction from the sum.
C++ Implementation to Swap Two Numbers Using Arithmetic Operators (Addition and Subtraction)
Below is the C++ implementation to swap two numbers using arithmetic operators (addition and subtraction):
Output:
Python Implementation to Swap Two Numbers Using Arithmetic Operators (Addition and Subtraction)
Below is the Python implementation to swap two numbers using arithmetic operators (addition and subtraction):
Output:
JavaScript Implementation to Swap Two Numbers Using Arithmetic Operators (Addition and Subtraction)
Below is the JavaScript implementation to swap two numbers using arithmetic operators (addition and subtraction):
Output:
How to Swap Two Numbers Using Arithmetic Operators (Multiplication and Division)
You can swap two numbers using multiplication and division in three simple steps:
Step 1: num1 = num1 * num2
Step 2: num2 = num1 /num2
Step 3: num1 = num1 / num2
Values of num1 and num2 are interchanged.
This is not a preferred method to swap two numbers because if either number is 0, the product of these two numbers will also be 0. Furthermore, if the 2nd number is 0, compilers will throw a division by zero error. Thus, you should avoid this approach to swap two numbers.
How to Swap Two Numbers Using Bitwise Operators
The bitwise XOR operator is used to swap two numbers.
C++ Implementation to Swap Two Numbers Using Bitwise Operators
Below is the C++ implementation to swap two numbers using XOR operators:
Output:
Python Implementation to Swap Two Numbers Using Bitwise Operators
Below is the Python implementation to swap two numbers using XOR operators:
Output:
JavaScript Implementation to Swap Two Numbers Using Bitwise Operators
Below is the JavaScript implementation to swap two numbers using XOR operators:
Output:
One-Line Solution to Swap Two Numbers in C++, Python, and JavaScript
You can also swap two numbers in one line without using any library functions.
C++ Implementation for One Line Solution
Output:
Python Implementation for One Line Solution
Output:
JavaScript Implementation for One Line Solution
Output:
If you want to have a look at the complete source code used in this article, here’s the GitHub repository.
Improve Your Programming Habits
If you want to improve your programming habits, you should follow certain programming principles like KISS (Keep It Simple, Stupid), Dry Code, YAGNI (You Aren’t Going to Need It), etc. But still, if you make some common coding mistakes, you ought to know about the most common coding mistakes. The knowledge will help you avoid common pitfalls and keep your code meaningful.