Problem Statement:
Illustration:
Input : m=9, n=5 Output : m=5, n=9 Input : m=15, n=5 Output : m=5, n=15 Here 'm' and 'n' are integer value
Approaches:
There are 3 different approaches to exchanging numbers which vary depending on the complexity of space and time.
Approaches are described below individually in same order as listed above:
Approach 1: Swapping the Values Using Third Variable
A memory cell will be created in the same type of memory that occupies the same memory in the memory stack area. During execution, it retains a value to replace other values, and once the required execution is completed, its value will be assigned to the second variable that already exists. Once the scope of the variable is three variables, it will be released from the memory unit. This variable is called a temporary variable, sometimes called a catalyst, because the participation in the output is not even tracked, but the execution will stop to produce the desired result seen above.
// Java Program to Swap Two values using third variable // using temp variable // Importing generic libraries import java.util.*; class GFG { // Function to swap two numbers // Using temporary variable static void swapValuesUsingThirdVariable(int m, int n) { // Swapping the values int temp = m; m = n; n = temp; System.out.println("Value of m is " + m + " and Value of n is " + n); } // Main driver code public static void main(String[] args) { // Random integerslues int m = 9, n = 5; // Calling above function to // reverse the numbers swapValuesUsingThirdVariable(m, n); } }
Output
Value of m is 5 and Value of n is 9
Approach 2 Exchange of values ??without using a third variable using the mathematical concepts of sum and difference.
Algorithms: There are 3 standard steps as listed below:
// Java Program to swap the two values // without using third variable // Importing generic Java libraries import java.util.*; class GFG { // Function to swap valus of two numbers // without creating temp variable static void swapValuesWithoutUsingThirdVariable(int m, int n) { // Steps as listed in algorithm // Difference of 2nd from 1st // is stored in first variable m = m - n; // Sum is stored in second variable n = m + n; // Difference of 1st from 2nd // is replaced in first variable m = n - m; // Print numbers System.out.println("Value of m is " + m + " and Value of n is " + n); } // Main driver method public static void main(String[] args) { // Random numbers of integer type int m = 9, n = 5; // Above function is called in main // to print swapped values of numbers swapValuesWithoutUsingThirdVariable(m, n); } }
Output
Value of m is 5 and Value of n is 9
Approach 3: Swapping the Values Using Operator
Illustration:
a = 5 = 0101 (In Binary) b = 7 = 0111 (In Binary) Bitwise XOR Operation of 5 and 7 0101 ^ 0111 ________ 0010 = 2 (In decimal)
This is the most optimized method, because the direct calculation here is done by bits instead of bytes, as seen in the above two methods. This is a Java program that shows the inner workings-
// Java Program to swap the two values // using XOR Operator // Importing generic Java libraries import java.io.*; class GFG { // Function to swap values of two numbers // using XOR operator static void swapValuesUsingXOROperator(int m, int n) { // Logic of XOR operator m = m ^ n; n = m ^ n; m = m ^ n; System.out.println("Value of m is " + m + " and Value of n is " + n); } // Main driver method public static void main(String[] args) { // Random two integer numbers // to get swapped int m = 9, n = 5; // Calling the function in main method // to get above integer numbers swapped swapValuesUsingXOROperator(m, n); } }
Output
Value of m is 5 and Value of n is 9
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.