In this post we are going to learn Swap two numbers with or without temporary variable in java
Example 1:
Java program to swap or exchange two numbers with using temporary variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java program to swap or exchange two numbers with using temporary variable */ public class ClientTest1 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { //Take two numbers from keyboard using Scanner System.out.println("Enter first Number:"); int firstNumber = scanner.nextInt(); System.out.println("Enter second Number:"); int secondNumber = scanner.nextInt(); System.out.println("Both numbers before swapping."); System.out.println("firstNumber ="+firstNumber+",secondNumber = "+secondNumber); //Logic to swap two numbers with using temporary variable int temp = firstNumber; firstNumber = secondNumber; secondNumber = temp; System.out.println("-------------------------------------------"); System.out.println("Both numbers after swapping."); System.out.println("firstNumber ="+firstNumber+",secondNumber = "+secondNumber); } catch (Exception e) { System.out.println("Please Enter valid number.."); e.printStackTrace(); } } } |
Example 2:
Java program to swap or exchange two numbers without using temporary variable.
Here We make use of Arithmetic Operator(+,-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java program to swap or exchange two numbers without using temporary variable */ public class ClientTest2 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Take two numbers from keyboard using Scanner System.out.println("Enter first Number:"); int firstNumber = scanner.nextInt(); System.out.println("Enter second Number:"); int secondNumber = scanner.nextInt(); System.out.println("Both numbers before swapping."); System.out.println("firstNumber ="+firstNumber+",secondNumber = "+secondNumber); //Here we have Logic to swap two numbers without using temporary variable firstNumber = firstNumber + secondNumber; secondNumber = firstNumber - secondNumber; firstNumber = firstNumber - secondNumber; System.out.println("-------------------------------------------"); System.out.println("Both numbers after swapping."); System.out.println("firstNumber ="+firstNumber+",secondNumber = "+secondNumber); } catch (Exception e) { System.out.println("Please Enter valid number.."); e.printStackTrace(); } } } |
Example 3:
In this java program swapping two numbers using bitwise XOR operator. Before going though the program, lets understand what is a bitwise XOR operator?
A bitwise XOR basically compares corresponding bits of two operands and it returns 1 if they are not equal and 0(zero) if they are equal. For example:
1 2 |
number1 = 11; /* equal to 00001011*/ number2 = 22; /* equal to 00010110 */ |
number1 ^ number2 compares corresponding bits of number1 and number2 and generates 1 if they are not equal, else it returns 0.
In our case it would return 29 which is equivalent to 00011101
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java program to swap or exchange two numbers using Bitwise XOR */ public class ClientTest3 { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { // Take two numbers from keyboard using Scanner System.out.println("Enter first Number:"); int firstNumber = scanner.nextInt(); System.out.println("Enter second Number:"); int secondNumber = scanner.nextInt(); System.out.println("Both numbers before swapping."); System.out.println("firstNumber ="+firstNumber+",secondNumber = "+secondNumber); //Logic to swap two numbers Using Bitwise XOR firstNumber = firstNumber ^ secondNumber; secondNumber = firstNumber ^ secondNumber; firstNumber = firstNumber ^ secondNumber; System.out.println("-------------------------------------------"); System.out.println("Both numbers after swapping."); System.out.println("firstNumber ="+firstNumber+",secondNumber = "+secondNumber); } catch (Exception e) { System.out.println("Please Enter valid number.."); e.printStackTrace(); } } } |
I have tested above programs for few inputs as below. You may check for other input as well
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter first Number: 10 Enter second Number: 20 Both numbers before swapping. firstNumber =10,secondNumber = 20 ------------------------------------------- Both numbers after swapping. firstNumber =20,secondNumber = 10 Enter first Number: -100 Enter second Number: 900 Both numbers before swapping. firstNumber =-100,secondNumber = 900 ------------------------------------------- Both numbers after swapping. firstNumber =900,secondNumber = -100 |
That’s all about this topic How to swap or exchange two numbers with or without using any temporary variable in java
You May Also Like:
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Java program to Remove Duplicate Elements From an Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
Java Program to find duplicate elements in an Array
Count number of words in a string in java
How would you check if a number is even or odd using bit wise operator in Java?
How can you check if the given number is power of 2?
How to check if String is number in java ?
If you have any feedback or suggestion please feel free to drop in blow comment box.
If you have any feedback or suggestion please feel free to drop in blow comment box.