3

How to Swap Two Variables in C++, Python, and JavaScript

 3 years ago
source link: https://www.makeuseof.com/swap-two-numbers/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

As a programmer, you've likely faced a situation that requires you to swap two numbers. Swapping two numbers is one of the most common situations programmers face while coding.

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:

#include <iostream>
using namespace std;
// Function to swap two numbers
// using a temporary variable
void swapNums(int num1, int num2)
{
// Printing numbers before swapping
cout << "Before Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
// Swapping with the help of a
// temporary variable "temp"
int temp = num1;
num1 = num2;
num2 = temp;
// Printing numbers after swapping
cout << "After Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
}
// Driver Code
int main()
{
swapNums(80, 50);
return 0;
}

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

Python Implementation to Swap Two Numbers Using a Temporary Variable

Below is the Python implementation to swap two numbers using a temporary variable:

# Function to swap two numbers
# using a temporary variable
def swapNums(num1, num2):
# Printing numbers before swapping
print("Before Swapping:")
print("num1: " , num1 , ", num2: " , num2)
# Swapping with the help of a
# temporary variable "temp"
temp = num1
num1 = num2
num2 = temp
# Printing numbers after swapping
print("After Swapping:")
print("num1: " , num1 , ", num2: " , num2)

# Driver Code
swapNums(80, 50)

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

JavaScript Implementation to Swap Two Numbers Using a Temporary Variable

Below is the JavaScript implementation to swap two numbers using a temporary variable:

<script>
// Function to swap two numbers
// using a temporary variable
function swapNums(num1, num2) {
// Printing numbers before swapping
document.write("Before Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
// Swapping with the help of a
// temporary variable "temp"
let temp = num1;
num1 = num2;
num2 = temp;
// Printing numbers after swapping
document.write("After Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
}
// Driver Code
swapNums(80, 50);
</script>

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

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):

#include <iostream>
using namespace std;
// Function to swap two numbers
// using arithmetic operators (+, -)
void swapNums(int num1, int num2)
{
// Printing numbers before swapping
cout << "Before Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
// Swapping with the help of
// artithmetic operators (+, -)
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
// Printing numbers after swapping
cout << "After Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
}
// Driver Code
int main()
{
swapNums(80, 50);
return 0;
}

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

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):

# Function to swap two numbers
# using arithmetic operators (+, -)
def swapNums(num1, num2):
# Printing numbers before swapping
print("Before Swapping:")
print("num1: " , num1 , ", num2: " , num2)
# Swapping with the help of
# arithmetic operators (+, -)
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
# Printing numbers after swapping
print("After Swapping:")
print("num1: " , num1 , ", num2: " , num2)

# Driver Code
swapNums(80, 50)

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

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):

<script>
// Function to swap two numbers
// using arithmetic operators (+, -)
function swapNums(num1, num2) {
// Printing numbers before swapping
document.write("Before Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
// Swapping with the help of
// using arithmetic operators (+, -)
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
// Printing numbers after swapping
document.write("After Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
}
// Driver Code
swapNums(80, 50);
</script>

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

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:

#include <iostream>
using namespace std;
// Function to swap two numbers
// using XOR operator
void swapNums(int num1, int num2)
{
// Printing numbers before swapping
cout << "Before Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
// Swapping with the help of
// XOR operator
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
// Printing numbers after swapping
cout << "After Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
}
// Driver Code
int main()
{
swapNums(80, 50);
return 0;
}

Output:

Before Swapping: 
num1 = 80, num2 = 50
After Swapping:
num1 = 50, num2 = 80

Python Implementation to Swap Two Numbers Using Bitwise Operators

Below is the Python implementation to swap two numbers using XOR operators:

Related: How to Write or Print to a File in Python

# Function to swap two numbers
# using XOR operator
def swapNums(num1, num2):
# Printing numbers before swapping
print("Before Swapping:")
print("num1: " , num1 , ", num2: " , num2)
# Swapping with the help of
# XOR operator
num1 = num1 ^ num2
num2 = num1 ^ num2
num1 = num1 ^ num2
# Printing numbers after swapping
print("After Swapping:")
print("num1: " , num1 , ", num2: " , num2)

# Driver Code
swapNums(80, 50)

Output:

Before Swapping:
num1: 80 , num2: 50
After Swapping:
num1: 50 , num2: 80

JavaScript Implementation to Swap Two Numbers Using Bitwise Operators

Below is the JavaScript implementation to swap two numbers using XOR operators:

<script>
// Function to swap two numbers
// using XOR operator
function swapNums(num1, num2) {
// Printing numbers before swapping
document.write("Before Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
// Swapping with the help of
// using XOR operator
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
// Printing numbers after swapping
document.write("After Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
}
// Driver Code
swapNums(80, 50);
</script>

Output:

Before Swapping:
num1: 80, num2: 50
After Swapping:
num1: 50, num2: 80

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

#include <iostream>
using namespace std;
int main()
{
int num1 = 80, num2 = 50;
cout << "Before Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
// One line solution to swap two numbers
num1 = num1 ^ num2, num2 = num1 ^ num2, num1 = num1 ^ num2;
cout << "After Swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
}

Output:

Before Swapping:
num1: 80, num2: 50
After Swapping:
num1: 50, num2: 80

Python Implementation for One Line Solution

num1 = 80
num2 = 50
print("Before Swapping:")
print("num1: " , num1 , ", num2: " , num2)
# One line solution to swap two numbers
num1, num2 = num2, num1
print("After Swapping:")
print("num1: " , num1 , ", num2: " , num2)

Output:

Before Swapping:
num1: 80, num2: 50
After Swapping:
num1: 50, num2: 80

JavaScript Implementation for One Line Solution

<script>
let num1 = 80, num2 = 50;
document.write("Before Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
// One line solution to swap two numbers
(num1 ^= num2), (num2 ^= num1), (num1 ^= num2);
document.write("After Swapping: <br>");
document.write("num1: " + num1 + ", num2: " + num2 + "<br>");
</script>

Output:

Before Swapping:
num1: 80, num2: 50
After Swapping:
num1: 50, num2: 80

Related: 10 Basic Programming Principles Every Programmer Must Know

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.

About The Author

6038eacecadb8-yuvraj%20profile%20pic.jpg?fit=crop&w=100&h=100

Yuvraj Chandra (18 Articles Published)

Yuvraj is a Computer Science undergraduate student at the University of Delhi, India. He's passionate about Full Stack Web Development. When he's not writing, he's exploring the depth of different technologies.

More From Yuvraj Chandra

Subscribe To Our Newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK