Easy Way to Swap Two Numbers in Java Without Variable

Introduction

Swapping two numbers is a common programming exercise that helps in understanding basic arithmetic operations and variable manipulation. In this task, we will swap two numbers without using a third variable, which demonstrates the clever use of arithmetic operations to achieve the goal.

Java Program to Swap Numbers Without Using a Third Variable

Below is a Java program that swaps two numbers without using a third variable:

Program

public class SwapNumbers {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;

        System.out.println("Before Swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // Swap using addition and subtraction
        a = a + b; // a now becomes 15
        b = a - b; // b becomes 5 (original value of a)
        a = a - b; // a becomes 10 (original value of b)

        System.out.println("After Swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}
Java

Steps to Swap Numbers Without Using a Third Variable

  1. Initial Assignment: Two integer variables a and b are initialized with values 5 and 10, respectively.
  2. Addition Step: Add a and b and store the result in a. Now, a holds the sum of both numbers.
  3. Subtraction Step 1: Subtract the new value of b from the current value of a and store the result in b. This effectively assigns the original value of a to b.
  4. Subtraction Step 2: Subtract the new value of b from the current value of a and store the result in a. This assigns the original value of b to a.
  5. Swapped Values: Now, a and b have swapped their values without using any additional variables.

Explanation of the Steps

  • Addition and Subtraction: By using addition and subtraction, we cleverly manipulate the variables to swap their values without needing a temporary variable. The idea is that when we add a and b together, the sum contains both values. Then, by subtracting one of the original values from this sum, we isolate the other value.
  • Arithmetic Operations: These operations are basic, yet powerful enough to achieve the swap. This technique is particularly useful when working with limited memory or in scenarios where using extra variables is not desirable.

Expected Output





Before Swapping:
a = 5
b = 10
After Swapping:
a = 10
b = 5
Java

How the Output is Produced

  1. Initial Values: The program first prints the initial values of a and b, which are 5 and 10.
  2. Swapping Process: The program performs the swapping using arithmetic operations as explained.
  3. Swapped Values: Finally, the program prints the values of a and b after swapping, showing that a is now 10 and b is 5.

Conclusion

Swapping two numbers without using a third variable is a clever trick that uses basic arithmetic operations. This technique is simple but powerful, especially when you want to minimize the use of additional variables. Understanding this approach enhances your problem-solving skills and provides insight into efficient coding practices.

Here are five great websites where you can learn Java:

  1. Oracle Java Tutorials – Official tutorials from Oracle that cover everything from basics to advanced Java topics.
  2. GeeksforGeeks Java – Comprehensive resource with tutorials, problems, and examples for all levels of Java programming.
  3. W3Schools Java Tutorial – Beginner-friendly tutorials with interactive examples and exercises.
  4. Java Programming on Codecademy – Interactive learning platform with hands-on coding exercises to practice Java.
  5. Java on Coursera – Offers various courses on Java, ranging from beginner to advanced levels, taught by university instructors and industry professionals.

These resources should help you get started and advance your Java programming skills!

Thanks For Visiting Our Website

Please rate our website(required)

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top