Java Program to Swap Two Numbers Without Using a Temporary Variable

Introduction

Swapping two numbers is a common task in programming. While it’s often done using a temporary variable, it’s also possible to swap two numbers without using any extra space. This can be accomplished using arithmetic operations or bitwise XOR. This guide will walk you through writing a Java program that swaps two numbers without using a temporary variable.

Problem Statement

Create a Java program that:

  • Prompts the user to enter two integers.
  • Swaps the values of the two integers without using a temporary variable.
  • Displays the swapped values.

Example:

  • Input: a = 5, b = 10
  • Output: "After swapping: a = 10, b = 5"

Solution Steps

  1. Read the Numbers: Use the Scanner class to take two integers as input from the user.
  2. Swap the Numbers Without Using a Temporary Variable: Use arithmetic operations or bitwise XOR to swap the numbers.
  3. Display the Result: Print the swapped values.

Java Program Using Arithmetic Operations

// Java Program to Swap Two Numbers Without Using a Temporary Variable // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class SwapNumbersWithoutTemp { public static void main(String[] args) { // Step 1: Read the numbers from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter the first number (a): "); int a = scanner.nextInt(); System.out.print("Enter the second number (b): "); int b = scanner.nextInt(); // Step 2: Swap the numbers using arithmetic operations a = a + b; // Now a contains the sum of a and b b = a - b; // Subtracting b from a gives the original value of a a = a - b; // Subtracting the new b from a gives the original value of b // Step 3: Display the swapped values System.out.println("After swapping: a = " + a + ", b = " + b); } } } 

Java Program Using Bitwise XOR

// Java Program to Swap Two Numbers Without Using a Temporary Variable Using XOR // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class SwapNumbersUsingXOR { public static void main(String[] args) { // Step 1: Read the numbers from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter the first number (a): "); int a = scanner.nextInt(); System.out.print("Enter the second number (b): "); int b = scanner.nextInt(); // Step 2: Swap the numbers using XOR a = a ^ b; // XOR the values of a and b, and store the result in a b = a ^ b; // XOR the new value of a with b to get the original value of a a = a ^ b; // XOR the new value of a with the new value of b to get the original value of b // Step 3: Display the swapped values System.out.println("After swapping: a = " + a + ", b = " + b); } } } 

Explanation

Step 1: Read the Numbers

  • The Scanner class is used to read two integer inputs from the user. The nextInt() method captures each number.

Step 2: Swap the Numbers Without Using a Temporary Variable

  • Using Arithmetic Operations:

    • a = a + b: This adds a and b together and stores the result in a.
    • b = a - b: This calculates the original value of a by subtracting b from the new value of a (which is the sum of a and b).
    • a = a - b: This calculates the original value of b by subtracting the new value of b from a.
  • Using Bitwise XOR:

    • a = a ^ b: This performs a bitwise XOR between a and b and stores the result in a.
    • b = a ^ b: This retrieves the original value of a by performing XOR between the new value of a and b.
    • a = a ^ b: This retrieves the original value of b by performing XOR between the new value of a and b.

Step 3: Display the Result

  • The program prints the swapped values using System.out.println().

Output Example

Example 1: Using Arithmetic Operations

Enter the first number (a): 5 Enter the second number (b): 10 After swapping: a = 10, b = 5 

Example 2: Using Bitwise XOR

Enter the first number (a): 20 Enter the second number (b): 30 After swapping: a = 30, b = 20 

Conclusion

These Java programs demonstrate how to swap two numbers without using a temporary variable. The solutions provided cover both arithmetic operations and bitwise XOR operations, offering multiple approaches to the problem. This is a valuable exercise for understanding basic programming concepts such as bitwise operations and arithmetic manipulation.

Leave a Comment

Scroll to Top