# [3 Ways] C Program to Swap Two Values Without using Temporary Variable

Swapping two variables is quite comfortable using a temporary variable. But to swap two values without using temporary variable makes the thing interesting.

### How to Swap two values without using temporary variable:

If you want to swap any two numbers, there are ways to swap the integer or float values by using some sort of arithmetic and bit manipulation operation.

**There are three ways for swapping numbers using…**

It looks messy at first, but when you look into these, it’s a just simple pattern to remember.

#### 1. Swapping Using Addition and Subtraction operation

x = x + y y = x - y x = x - y

Addition and subtraction are arithmetic operations. This method can apply for swapping integer as well as floating type values.

#include<stdio.h>; int main() { // intialise two variables int x = 25, y = 17; // Arithmatic Code to swap 'x' and 'y' x = x + y; // after addition x with contain 42 y = x - y; // y will contanis 25 x = x - y; // x becomes 17 printf("After Swapping two values: x = %d, y = %d", x, y); return 0; }

**Output:**

After Swapping two values: x = 17, y = 25

This method is easy to understand as like arithmetic addition and subtraction methods.

#### 2. Swapping Using Multiplication and Division operation

x = x * y y = x / y x = x / y

#include<stdio.h>; int main() { // intialise two variables int x = 25, y = 17; // Arithmatic multiplicatio and division Code to swap 'x' and 'y' x = x * y; // after addition x with contain 42 y = x / y; // y will contanis 25 x = x / y; // x becomes 17 printf("After Swapping two values: x = %d, y = %d", x, y); return 0; }

It is similar to the first method. Only difference is that it uses multiplication operation instead of addition and division operation instead of subtraction.

**Output:**

After Swapping two values: x = 17, y = 25

#### 3. Swapping two values Using XOR operation

x = x ^ y y = x ^ y x = x ^ y

**Note:** If you are swapping two values using XOR bit operation, it works for integers only.

#include<stdio.h>; int main() { // intialise two variables int x = 25, y = 17; // XOR Bit operation Code to swap 'x' and 'y' x = x ^ y; // after addition x with contain 42 y = x ^ y; // y will contanis 25 x = x ^ y; // x becomes 17 printf("After Swapping two values: x = %d, y = %d", x, y); return 0; }

**Output:**

After Swapping two values: x = 17, y = 25

To remember, XOR bit operation method is easy as it uses a single operation.

I have described all these methods by writing code in C and C++. You can use all the above three methods to write a program in any language to swap two values without using temporary variable.

If you are Python developer, you don’t need to this as well. You can write Python program to swap two variables without using third variable by a just single line of code.

Do you know any other method to swap two variables? You can write in the comment section below. I would like to mention that in this post as well.