We will learn how to swap two numbers in java in this tutorial . We can write swapping program in java using different methods .
1. Swapping of two numbers In Java using third variable
First we will write java program to swap two numbers using third variable . In this java program we use a third variable to swap two numbers . First we take input from user for variable i & j then with the help of third variable t we will swap these two numbers i & j.
import java.util.Scanner;
public class SwapTwoNumberUsingVariable {
public static void main(String[] args) {
int i, j, t;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
t = i;
i = j;
j = t;
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
sc.close();
}
}
Output :
Enter the value of i :
10
Enter the value of j :
20
Before swapping numbers i=10, j=20
Before swapping numbers i=20, j=10
2. Swapping Program In Java Using Addition and Subtraction Operator
We will write a program to swap two numbers in java using Addition and Subtraction Operator . In this java program we will swap of two numbers without using third variable in java . We swap two numbers using Addition and Subtraction Operator by below logic
i= i -j;
j = i+j;
i = j-i;
Let’s write code this
import java.util.Scanner;
public class SwapTwoNumberUsingAdd {
public static void main(String[] args) {
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
i= i -j;
j = i+j;
i = j-i;
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
sc.close();
}
}
Output:
Enter the value of i :
10
Enter the value of j :
20
Before swapping numbers i=10, j=20
Before swapping numbers i=20, j=10
3. Swapping of two numbers in java Using Multiplication and Division Operator
This example also for swap of two numbers without using third variable in java . In this example we take input from user and then program to swap two numbers using Multiplication and Division Operator as below .
import java.util.Scanner;
public class SwapTwoNumberUsingmul2 {
public static void main(String[] args) {
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
//swapping two number in one line
i = i*j /(j=i);
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
sc.close();
}
}
Output :
Enter the value of i :
10
Enter the value of j :
20
Before swapping numbers i=10, j=20
Before swapping numbers i=20, j=10
4. Swapping of two numbers in java Using XOR
Now write a program to swap two numbers using XOR . In this example we will take input from user ans then swap numbers using XOR. In this we swap of two numbers without using third variable in java . Let’s see example swapping of two numbers in java using xor .
import java.util.Scanner;
public class SwapTwoNumberUsingXOR {
public static void main(String[] args) {
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swap i=" + i + ", j=" + j);
i = i ^ j;
j = i ^ j;
i = i ^ j;
System.out.println("After Swap i=" + i + ", j=" + j);
sc.close();
}
}
Output :
Enter the value of i :
10
Enter the value of j :
20
Before swap i=10, j=20
After Swap i=20, j=10
We have seen how to swap two numbers in java using different method. In this tutorial we learned swapping of two numbers using third variable and also swap of two numbers without using third variable in java . you can learn more java program for practice .