SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Swapping Two Numbers Without Third Variable Program in Java and Python

18 October 2012

Java and Python program to swap two integer values without using a third variable by using the bitwise XOR operator.

Question:

Write a program to input two integers and swap their values without using any third variable.

In this method, use the bitwise XOR operator, represented by the caret symbol ^. The values must be displayed before and after swapping.

Example

INPUT: a = 25 b = 13 OUTPUT: The numbers before swapping are a = 25 b = 13 The numbers after swapping are a = 13 b = 25

Algorithm:

Step 1: Start.

Step 2: Create a Scanner object to accept input from the user.

Step 3: Accept the first integer and store it in a.

Step 4: Accept the second integer and store it in b.

Step 5: Display the values of a and b before swapping.

Step 6: Store the XOR of both values in a by writing a = a ^ b.

Step 7: Recover the original value of a into b by writing b = a ^ b.

Step 8: Recover the original value of b into a by writing a = a ^ b.

Step 9: Display the values of a and b after swapping.

Step 10: Stop.

Explanation:

This program swaps two integer values without using an extra temporary variable. The method used here is based on the bitwise XOR operator. XOR works on the binary form of numbers. For each bit position, XOR gives 1 when the two bits are different and 0 when the two bits are the same. An important property of XOR is that if a value is XORed with another value twice, the original value comes back.

Suppose the two numbers are stored in a and b. In the first step of swapping, a = a ^ b stores a combined XOR value in a. At this point, b is still unchanged. In the second step, b = a ^ b means b becomes (old a ^ old b) ^ old b. Since old b ^ old b cancels to 0, the result becomes the old value of a. So b now contains the original value of a.

In the third step, a = a ^ b is performed again. Now a still contains the combined XOR value and b contains the old value of a. Therefore a becomes (old a ^ old b) ^ old a, which leaves the old value of b. After these three statements, the values have been exchanged.

For example, if a = 25 and b = 13, the first XOR gives an intermediate value. The second XOR places 25 in b, and the third XOR places 13 in a. Thus the values are swapped without declaring a third variable. This method is suitable only for integer values because bitwise XOR works on integer binary representation.

Java Program:

Java
/**
* The class Swapping_Method1 takes two numbers as input
* and swaps their values without using a third variable.
* This method uses the bitwise XOR operator.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Swapping_Method1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the 1st no: ");
        int a = sc.nextInt();

        System.out.print("Enter the 2nd no: ");
        int b = sc.nextInt();

        System.out.println("-------------------------------");
        System.out.println("The numbers before swapping are");
        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Beginning of swapping using XOR.
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        // End of swapping.

        System.out.println("-------------------------------");
        System.out.println("The numbers after swapping are");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

Equivalent Python Program:

Python
# Program to swap two numbers without using a third variable.
# This method uses the bitwise XOR operator.

a = int(input("Enter the 1st no: "))
b = int(input("Enter the 2nd no: "))

print("-------------------------------")
print("The numbers before swapping are")
print("a =", a)
print("b =", b)

# Beginning of swapping using XOR.
a = a ^ b
b = a ^ b
a = a ^ b
# End of swapping.

print("-------------------------------")
print("The numbers after swapping are")
print("a =", a)
print("b =", b)

Output:

Enter the 1st no: 25 Enter the 2nd no: 13 ------------------------------- The numbers before swapping are a = 25 b = 13 ------------------------------- The numbers after swapping are a = 13 b = 25

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →