Swapping of two numbers in Java
In this post, you will learn how to swap two numbers using the Java programming language.
Here, we have mentioned two ways to swap two numbers. First program uses a temporary variable, while second program does not use any temporary variable.
Algorithm
STEP 1: START
STEP 2: DEFINE x, y, temp
STEP 3: ENTER x, y
STEP 4: PRINT x, y
STEP 5: temp = x
STEP 6: x= y
STEP 7: y= temp
STEP 8: PRINT x, y
STEP 9: END
Java Swap two numbers using temporary variable
In the given Java program, we use a temporary variable, 'temp' to swap two numbers. First, we assign the value of 'x' to 'temp'. Then, assign the value of 'y' to 'x' and the value of 'temp' to 'y'. Finally, the value of 'x' will be assigned to 'y' and the value of 'y' will be assigned to 'x'.
// Swapping of two numbers
public class SwapNumbers
{
public static void main(String[] args)
{
int temp;
int x = 100;
int y = 200;
// displaying values
System.out.println("x = " + x + " and y = " + y +"\n");
//Swapping numbers
temp = x;
x = y;
y = temp;
// displaying swapped values
System.out.println("x = " + x + " and y = " + y);
}
}
Output of the above code:
x = 100 and y = 200
x = 200 and y = 100
Java Swap two numbers without using temporary variable
In the given example, 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 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
Java Swap two numbers using Multiplication and division
In the given example, 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
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