## Introduction

We all started our coding journey by writing the famous “Hello World!” program and some basic ones like swapping two numbers. Developing good coding skills need practice. You simply can't go to the coding interview and try to solve the coding tasks in a short time. That's one of the most frequent reasons coding job interviews fail.

In a telephonic round, the interviewer may ask slightly simpler code problems, such as how to swap two numbers without using third variable.

This article discusses how to write code to swap two numbers without using third variable. So let’s start.

### Problem Statement

Ninja Coder gave you two numbers; your task is to swap two numbers without using third variable.

Before jumping to the solution directly, we recommend solving the question in __Coding Ninjas Studio Problems Section.__

## Approach 1: Swap two numbers by using arithmetic operators.

Our first approach to swap two numbers without using third variable is by using **Arithmetic Operators**. We can solve this problem in two ways:

- Using Addition and Subtraction Operators.
- Using Multiplication and Division Operators.

### Algorithm

For using the first method to swap two numbers without using third variable, follow the given steps:

**🧩 Step 1:** Find the Sum of the given two numbers and store it in one of the two given numbers.

🧩** Step 2:** We can use the sum and subtraction from the sum to swap two numbers without using third variable.

### Implementation in C++

**Output**

```
Enter the value of number a = 23
Enter the value of number b = 3
After Swapping two numbers: a = 3, b= 23
```

Similarly, for using the second method to swap two numbers without using third variable, follow the given steps:

**🧩 Step 1: ** Find the product of the given two numbers and store it in one of the two given numbers.

**🧩 Step 2: **We can use the product and division of the product to swap two numbers without using third variable.

### Implementation in C++

**Output**

```
Enter the value of number a = 23
Enter the value of number b = 3
After Swapping two numbers: a = 3, b= 23
```

Try and compile with __online c++ compiler__.

#### Complexity Analysis

**Time Complexity: **O(1); since we are using arithmetic operators, these operators take constant time. So, the time complexity is constant.

**Space Complexity: **O(1); since we are not using any extra space to store the numbers, the space complexity is also constant.