In this tutorial, we are going to learn new program which is a simple program and most commonly used in exams or in interviews that is **Swap two numbers without using third variable**.

Implementation of this program is very simple.

**What is swapping:** Swapping two number means replacing the numbers.

**How to swap two numbers without using third variable?**

For swapping number first we have 2 numbers and without using third variable we swap numbers by doing a simple mathematical operation (sum and subtract )

**Steps for swapping two numbers without using third variable**

Let us assume variable a, b

1. Add first and second number and store addition in the first variable.

**a = a + b**

2. Subtract first and second number and store it in the second variable.

** b = a – b**

3. Subtract first and second number and store it in the first variable.

** a = a – b**

**Also Read:** C program swap two number using third variable

**Example:**

First number = 10

Second number = 20

Swap numbers without using third variable by following above steps

1. first number = first number + second number

=> first number = 10 + 20

=> first number = 30

2. second number = first number – second number

=> second number = 30 – 20

=> second number = 10

3. first number = first number – second number

=> first number = 30 – 10

=> first number = 20

Now we got swapped numbers

First number = 20

Second number = 10

In this way using simple addition and subtract we perform swapping without using third variable.

Now, let’s see the program for this.

### C Program to swap two numbers without using third variable

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<stdio.h> void main() { int first,second ; printf("Enter first\n"); scanf("%d",&first); printf("Enter second\n"); scanf("%d",&second); printf("before swaping\n first =%d \n second =%d\n"first, second); first = first + second; second = first - second; first = first - second; printf(" after swaping\n first=%d\n second=%d",first,second); } |

**Output:**

**Enter first**

**10**

**Enter second**

**20**

**before swapping**

**first =10**

**second = 20**

** after swapping**

**first = 20**

**second =10**

**Explanation**:

1. First Here we initialized the first and second variable.

first —> for storing 1st number

second –>for storing the second number

2. Now here goes the logic(let us take first = 10 and second=20)

first = first + second , Therefore, first =10+20 =30

second = first – second , Therefore second = 30 – 20 =10

first = first – second, Therefore first =30 – 10 = 20

3. Now the values of first and second are 20 and 10

4. The values of first and second are printed.