Here is an explaination on How to swap 2 no using 3rd variable Program in java.
import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
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);
temp = x;
x = y;
y = temp;
System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}
}
Let’s work out the Java program to understand what it does.
Step-by-Step Explanation of How to swap 2 no using 3rd variable Program in java
1. import java.util.Scanner;
The above statement is an import of the Scanner class of the java.util package. The Scanner class helps to read user input from the console.
2. class SwapNumbers
This line declares a class called SwapNumbers. This is the main class that will contain the logic for swapping two numbers.
3. public static void main(String args[])
This is the major method, and this is, in fact, the entry point of any Java program.
- public: this is the ability to access that method from almost everywhere, in other words, required by the JVM to run the application.
- static: This ability is to have a method callable without an instantiation of the SwapNumbers class.
- void: This indicates that the method does not return any value.
- String[] args: This is a parameter that holds command-line arguments, though it is not used in this program.
4. int x, y, temp;
Here, three integer variables are declared:
- x: It will store the first number typed by the user.
- y: This is the second number that the user will type.
- temp: It is a temporary variable that helps in swapping the values of x and y.
5. System.out.println(“Enter x and y”);
It’s a statement that prints to the console with a message asking for an integer input and two further input integers named x and y.
6. Scanner in = new Scanner(System.in)
Declares a Scanner variable called in that will read input from the console, or in other words, the keyboard.
7. x = in.nextInt(); and y = in.nextInt();
- These two lines of code read two integer values from the user input, which are then stored inside the variables x and y, respectively.
- nextInt(): This is a method in the Scanner class that reads the next integer from the input.
8. System.out.println(“Before Swapping” +”
x = ” + x +” “+ “y = ” + y);
- This statement prints the values of x and y before the swapping operation.
- The \ makes sure that x and y are printed on two different lines.
9. Swap the values of x and y:
temp = x;
x = y;
y = temp;
- temp = x;: Store the value of x in temp. We need to save the original value of x before overwriting it.
- x = y;: Assign the value of y to x.
- y = temp;: Assign the original value of x (saved in temp) to y.
This is the final step of value swapping between x and y.
10. System.out.println(“After Swapping”+x + “+y = ” + y);
This line prints the values of x and y after the swapping operation, thus showing the result to the user.
11. Closing curly braces }
The closing curly braces are the end of the main method and the SwapNumbers class.
Example of Program Execution:
Consider that the user is going to give these inputs.
x = 5
y = 10
Code Explanation:
1. The code requests the user to input two integers.
2. The user enters 5 for the value of x and 10 for the value of y.
3. The program displays the values of x and y before the swapping statement is encountered.
Before Swapping
x = 5
y = 10
4. The values of x and y are swapped.
5. The program prints the values of x and y after swapping:
After Swapping
x = 10
y = 5
Output:
For the input values x = 5 and y = 10, the output will be:
Enter x and y
5
10
Before Swapping
x = 5
y = 10
After Swapping
x = 10
y = 5
This confirms that the numbers have indeed been swapped.