/*Swap Two Numbers Using Third Variable.*/
import java.util.Scanner;
class SwapTwoNumber
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter value 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);
}
}
● Save the file as SwapTwoNumber.java.import java.util.Scanner;
class SwapTwoNumber
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter value 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);
}
}
● When the program is run, the following output is displayed:
Output:
Enter x and y 12
14
Before Swapping
x := 12
y := 14
After Swapping
x := 14
y := 12