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

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

March 9, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Friday, 1 May, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

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

by ITECHNEWS
March 9, 2022
in Data Science, Leading Stories
0 0
0
How to Swap Two Variables in C++, Python, and JavaScript

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.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

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:

 

# 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

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.

Source: YUVRAJ CHANDRA
Via: makeuseof
Tags: and JavaScriptPythonVariables in C++
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version