Swapping of two numbers without using third variable in Java
In this post, you will learn different ways of swapping two numbers using the Java programming language. Such a type of question is generally asked in programming interviews. The interviewer may ask you to write a program for the swapping of two numbers using a particular method.
Swap two numbers using addition and subtraction in Java
In the given Java program, we swap two numbers without using the temporary variable. We define only two variables. We use any one variable from a given two variables to store the sum of both variables. Then, we use the subtraction operation to get the values swapped.
// Swapping of two numbers
public class SwapNumbers
{
public static void main(String[] args)
{
int x = 500;
int y = 400;
// Displaying values
System.out.println("x = " + x + " and y = " + y);
//Swapping in steps
x = x + y; //x = 500 + 400 = 900
y = x - y; //y = 500 - 400 = 100
x = x - y; //x = 500 - 400 = 100
//Verifing swapped values
System.out.println("x = " + x + " and y = " + y);
}
}
Output of the above code:
x = 500 and y = 400
x = 400 and y = 500
Swap two numbers using Multiplication and division in Java
In the given Java program, we swap two numbers using the arithmetic operators multiplication and division.
import java.io.*;
public class SwappingNumbers {
public static void main(String[] args)
{
int x = 200;
int y = 100;
System.out.println("Before swaping:"
+ " x = " + x + ", y = " + y);
// swapping 'x' and 'y' using
// multiplication and division
x = x * y;
y = x / y;
x = x / y;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
Output of the above code:
Before swaping: x = 200, y = 100
After swaping: x = 100, y = 200
Java program to swap two numbers using Bitwise XOR
Here, we have used the bitwise XOR operator to swap two variables. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever x and y differ. For example, XOR of 10 (binary 1010) and 5 (binary 0101) is 1111.
import java.util.Scanner;
public class SwapTwoNumbers
{
public static void main(String args[])
{
int x, y;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
x = scanner.nextInt();
System.out.print("Enter the second number: ");
y = scanner.nextInt();
System.out.println("Before swapping:");
System.out.println("x = " +x +", y = " +y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println("After swapping:");
System.out.print("x = " +x +", y = " +y);
}
}
Output of the above code:
Enter the first number: 62
Enter the second number: 90
Before swapping:
x = 62, y = 90
After swapping:
x = 90, y = 62
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java