Swap Two Numbers: In 5 Different Ways
Navnath Jadhav
3 min read
Table of contents
- <mark>Program to swap two numbers</mark>
- <mark>Solution:</mark>
- Language Used: Java
- Approach 1: swap two numbers using the third variable
- Approach 2: swap two numbers without using a third variable using + and -
- Approach 3: swap two numbers using * and /
- Approach 4: swap two numbers using bitwise X-OR
- Approach 5: swap two numbers using a single statement
Program to swap two numbers
Input:
Enter the value of X and Y
num1=10
num2=20
Before swapping: 10 20
Output:
num1=20
num2=10
After swapping: 20 10
Solution:
Language Used: Java
Approach 1:
swap two numbers using the third variable
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
// swap two numbers
Scanner s = new Scanner(System.in) ;
System.out.println("Enter first number");
int num1 = s.nextInt() ;
System.out.println("Enter second number");
int num2 = s.nextInt() ;
System.out.println("Before swapping: "+num1+" "+num2);
logic-1 - swap two numbers using third variable
int temp;
temp = num1; //10
num1 = num2; //20
num2 = temp; //10
System.out.println("After swapping: "+num1+" "+ num2);
}
}
Approach 2:
swap two numbers without using a third variable using + and -
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
// swap two numbers
Scanner s = new Scanner(System.in) ;
System.out.println("Enter first number");
int num1 = s.nextInt() ;
System.out.println("Enter second number");
int num2 = s.nextInt() ;
System.out.println("Before swapping: "+num1+" "+num2);
// logic-2 - swap two numbers without using third variable using + and -
num1=num1+num2 ;
num2=num1-num2 ;
num1=num1-num2 ;
System.out.println("After swapping: "+num1+" "+ num2);
}
}
Approach 3:
swap two numbers using * and /
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
// swap two numbers
Scanner s = new Scanner(System.in) ;
System.out.println("Enter first number");
int num1 = s.nextInt() ;
System.out.println("Enter second number");
int num2 = s.nextInt() ;
System.out.println("Before swapping: "+num1+" "+num2);
//logic - 3 - using * and /
num1=num1*num2 ;
num2=num1/num2 ;
num1=num1/num2 ;
System.out.println("After swapping: "+num1+" "+ num2);
}
}
Approach 4:
swap two numbers using bitwise X-OR
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
// swap two numbers
Scanner s = new Scanner(System.in) ;
System.out.println("Enter first number");
int num1 = s.nextInt() ;
System.out.println("Enter second number");
int num2 = s.nextInt() ;
System.out.println("Before swapping: "+num1+" "+num2);
// logic-4 using bitwise X-OR
num1=num1^num2 ;
num2=num1^num2 ;
num1=num1^num2 ;
System.out.println("After swapping: "+num1+" "+ num2);
}
}
Approach 5:
swap two numbers using a single statement
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
// swap two numbers
Scanner s = new Scanner(System.in) ;
System.out.println("Enter first number");
int num1 = s.nextInt() ;
System.out.println("Enter second number");
int num2 = s.nextInt() ;
System.out.println("Before swapping: "+num1+" "+num2);
// logic-5 - use single statement
num2=num1+num2-(num1=num2) ;
System.out.println("After swapping: "+num1+" "+ num2);
}
}
0
Subscribe to my newsletter
Read articles from Navnath Jadhav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Navnath Jadhav
Navnath Jadhav
I am a Software Developer