# Program to Swap Two Numbers Without Using Third Variable | FACE Prep

In this article, we will be discussing the program to swap two numbers without using third variable in C, C++, Java. There are 5 methods to do swap two numbers without using a temporary variable. Below are the methods.

## Method 1: Using Arithmetic operators + and –

`#include<stdio.h>int main(){    int a, b;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);  //consider two numbers as 4 and 5    a = a + b;  //a = 4 + 5 = 9    b = a - b;  //b = 9 - 5 = 4    a = a - b;  //a = 9 - 4 = 5    printf("Numbers after swapping: %d %d", a, b);}`
`#include<iostream>using namespace std;int main(){    int a, b;    cout << "Enter two numbers: ";    cin >> a >> b;  //consider two numbers as 4 and 5    a = a + b;  //a = 4 + 5 = 9    b = a - b;  //b = 9 - 5 = 4    a = a - b;  //a = 9 - 4 = 5    cout << "Numbers after swapping: " << a << " " << b;}`
`import java.util.Scanner;public class Main{    public static void main(String args[])    {        Scanner sc = new Scanner(System.in);        System.out.println("Enter two numbers: ");        //consider two numbers as 4 and 5        int a = sc.nextInt();         int b = sc.nextInt();        a = a + b;  //a = 4 + 5 = 9        b = a - b;  //b = 9 - 5 = 4        a = a - b;  //a = 9 - 4 = 5        System.out.println("Numbers after swapping: " + a + " " + b);    }}`

Quick Tip:

Instead of the above logic, you can also use

`a = b - a;b = b - a;a = b + a;`

No wonder! You can even swap using a single line logic.

`a = (a + b) - (b = a);`

## Method 2: Using Arithmetic operators * and /

`#include <stdio.h>int main(){    int a, b;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);  //consider two numbers as 4 and 5    a = a * b;  // a = 4 * 5 = 20    b = a / b;  // b = 20 / 5 = 4    a = a / b;  // a = 20 / 4 = 5    printf("Numbers after swapping: %d %d", a, b);}`
`#include <iostream>using namespace std;int main(){    int a, b;    cout << "Enter two numbers: ";    cin >> a >> b;  //consider two numbers as 4 and 5    a = a * b;  // a = 4 * 5 = 20    b = a / b;  // b = 20 / 5 = 4    a = a / b;  // a = 20 / 4 = 5    cout << "Numbers after swapping: " << a << " " << b;}`
`import java.util.Scanner;public class Main{    public static void main(String args[])    {        Scanner sc = new Scanner(System.in);        System.out.println("Enter two numbers: ");        //consider two numbers as 4 and 5        int a = sc.nextInt();         int b = sc.nextInt();        a = a * b;  // a = 4 * 5 = 20        b = a / b;  // b = 20 / 5 = 4        a = a / b;  // a = 20 / 4 = 5        System.out.println("Numbers after swapping: " + a + " " + b);    }}`

Quick Tip:

Logic in single line: a = (a * b) / (b = a);

Note: The method using arithmetic operators * and / will not work if a or b is 0.

## What is Arithmetic overflow?

Method 1 & 2 may cause an arithmetic overflow when the values of a & b are significantly large and here is what we mean by it.

Arithmetic overflow occurs when the result of an arithmetic operation is a value that is too large to fit in the available storage space.

Consider 3 four-bit variables a, b, c. The value stored in the variables a and b is 15 and 1 respectively. We need to add the two variables and store the result in the variable ‘c’.

15 in Binary —>  1111
1 in Binary   —>   0001
———————————
c   =    10000
———————————

After adding, the result is 16, we get a 5 bit variable and this is too large to fit in 4-bit variable ‘c’. That is how arithmetic overflow occurs.

## Method 3: Using Bit-wise Operators

##### The truth table of Bitwise X-OR operator:
 a b Output 0 0 0 0 1 1 1 0 1 1 1 0

In the line a = a ^ b, the value 4 is X-ORed with 5 and the result is stored in the variable ‘a’. Let’s see the X-OR operation of 4 and 5.

The binary format of 4 is 100
The binary format of 5 is 101

——–
001
——–

Answer after performing bitwise X-OR is 1 and that is stored in the variable ‘a’.

Next, in the line b = a ^ b, the value 1 is X-ORed with 5 and the result is stored in the variable ‘b’. Let’s the X-OR operation of 1 and 5.

The binary format of 1 is 001
The binary format of 5 is 101

——–
100
——–

Answer after performing bitwise X-OR is 4 and that is stored in the variable ‘b’.

Next, in the line a = a ^ b, the value 1 is X-ORed with 4 and the result is stored in the variable ‘a’. Let’s the X-OR operation of 1 and 4.

The binary format of 1 is 001
The binary format of 4 is 100

——-
101
——-

Answer after performing bitwise X-OR is 5 and that is stored in the variable ‘a’.

Thus both the values got swapped using X-OR operator.

`#include <stdio.h>int main(){    int a, b;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);     a = a ^ b;     b = a ^ b;    a = a ^ b;    printf("Numbers after swaping: %d %d", a, b);}`
`#include <iostream>using namespace std;int main(){    int a, b;    cout << "Enter two numbers: ";    cin >> a >> b;    a = a ^ b;     b = a ^ b;     a = a ^ b;     cout << "Numbers after swapping: " << a << " " << b;}`
`import java.util.Scanner;public class Main{    public static void main(String args[])    {        Scanner sc = new Scanner(System.in);        System.out.println("Enter two numbers: ");        int a = sc.nextInt();         int b = sc.nextInt();        a = a ^ b;         b = a ^ b;         a = a ^ b;         System.out.println("Numbers after swapping: " + a + " " + b);    }}`