Here is an explaination on How to swap 2 no without using 3rd variable Program in Java
import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}
}
Let’s understand how the following Java program works step by step.
Step-by-Step of Explanation of How to swap 2 no without using 3rd variable Program in Java
1. import java.util.Scanner;
It imports the class Scanner from the java.util package, which is used to read the input from the user (in this case, the keyboard).
2. class SwapNumbers
This line defines a class named SwapNumbers. This class contains the logic to swap two numbers entered by the user.
3. public static void main(String args[])
This is the main method, which is the entry point of any Java program.
- public: The access modifier is set to public, therefore the method could be accessed everywhere and it had to be since the JVM wants to run a program.
- static: It is a static method. Therefore, it is called without actually creating an instance of the class.
- void: It does not have an explicit return value.
- String[] args: This argument accepts any number of command line arguments supplied to the program, though it is not used in this program.
4. int x, y;
Declare two integer variables x and y. These will store the two numbers the user inputs and will be swapped.
5. System.out.println(“Enter x and y”);
This statement prints the text “Enter x and y”, followed by a user prompt for two numbers to x and y.
6. Scanner in = new Scanner(System.in);
It makes a Scanner object called in, which scans input from the console keyboard.
7. x = in.nextInt(); y = in.nextInt();
- These two statements read the integer inputs given by the user and place them in the variables x and y, respectively.
- nextInt(): This reads the next integer from the input.
8. System.out.println(“Before Swapping\nx= “+x+”\ny = “+y”);
This prints the values of x and y before swapping. The ‘\n’ helps in printing values on different lines.
9. Swap Logic (Without using a temp variable):
The swapping logic is performed without using a temporary variable by using arithmetic operations:
x = x + y; // Step 1: x now holds the value of x + y
y = x - y; // Step 2: y holds the value of x from before
x = x - y; // Step 3: x = y's value before
- Step 1: x = x + y;
At this step, x is assigned the value that results from adding x and y. Assuming that x = 5 and y = 10, now x is 15.
- Step 2: y = x – y;
Here y is assigned its former value which was x. Therefore y now equals 15 – 10 = 5 which happens to be xs previous value.
- Step 3: x = x – y;
Ultimately, x is set equal to the initial value of y. Hence, x will take the form of 15 – 5 = 10, which is the original value of y.
This is how the swapping operation is carried out without needing a temporary variable.
10. System.out.println(“After Swapping”x = ” + x + ” \ty = ” + y”);
This prints the value of x and y after swapping. The output will print the values in a swapped manner.
11. Closing curly braces }
The closing curly braces show the termination of the main method and the SwapNumbers class.
Example of Program Execution:
Assume that the user enters the following values for x and y:
x = 5
y = 10
Execution Explanation:
1. The program prompts the user to enter two integers.
2. The user enters 5 for x and 10 for y.
3. The program prints:
Before Swapping
x = 5
y = 10
4. The program swaps the values of x and y using the arithmetic method.
5. The program prints the swapped values:
After Swapping
x = 10
y = 5
Output
For the input values given, x = 5 and y = 10, the following is the output :
Enter x and y
5
10
Before Swapping
x = 5
y = 10
After Swapping
x = 10
y = 5
This shows that the values of x and y have been swapped correctly using arithmetic operations without using a temporary variable.