Swap Two Numbers in Java

Exercise:

Write a Java Program that saves two numbers as variables, prints them both, and then swaps the values held by both variables and re-prints their values onto the screen.

1.Swap using temporary variables.

Click Here to View the Solution!
 public class SwappingNumbers {

     public static void main(String[] args) {
  
         float firstNum = 7.30f, secondNum = 9.45f;
  
         System.out.println("****BEFORE SWAP****");
         System.out.println("First number = " + firstNum);
         System.out.println("Second number = " + secondNum);
  
         // Assigned value of firstNum to temporary variable
         float temporary = firstNum;
  
         // Assigned value of secondNum to firstNum
         firstNum = secondNum;
  
         // Assigned value of temporary (which contains the initial value of firstNum) to secondNum
         secondNum = temporary;
  
         System.out.println("****AFTER SWAP****");
         System.out.println("First number = " + firstNum);
         System.out.println("Second number = " + secondNum);
     }
 } 
Click Here to View the Output!
****BEFORE SWAP****
 First number = 7.3
 Second number = 9.45
 ****AFTER SWAP****
 First number = 9.45
 Second number = 7.3
Click Here to View the Explanation!
  • This program involves swapping two numbers, 7.30 and 9.45, both float numbers, by using a temporary variable and displays the swapped values.
  • The two numbers to be swapped are placed in variables named as firstNum and secondNum.
  • Initially, the printing of the values stored in the variables is done using println() to ensure the output after the swapping occurs.
  • Now the temporary variable comes into play, the value of the variable firstNum is stored into the temporary variable. Hence temporary = 7.30
  • Then place the value of the variable secondNum into the variable firstNum. Hence firstNum = 9.45
  • The last step is eventually the storing of the value of the variable temporary into the variable secondNum. Hence secondNum = 1.20
  • The program ends after this swapping process and the swapped values are displayed on the screen.
  • It is to be noted that the variable temporary is utilized only to store the initial value of the variable firstNum before the process of swapping takes place. However, swapping can be done without using a temporary variable too.

2.Swap Without using temporary variables.

Click Here to View the Solution!
public class SwappingNumbers {
 
    public static void main(String[] args) {
 
        float firstNum = 9.0f, secondNum = 31.6f;
 
        System.out.println("****BEFORE SWAP****");
        System.out.println("First number = " + firstNum);
        System.out.println("Second number = " + secondNum);
 
        firstNum = firstNum - secondNum;
        secondNum = firstNum + secondNum;
        firstNum = secondNum - firstNum;
 
        System.out.println("****AFTER SWAP****");
        System.out.println("First number = " + firstNum);
        System.out.println("Second number = " + secondNum);
    }
} 
Click Here to View the Output!
****BEFORE SWAP****
 First number = 9.0
 Second number = 31.6
*****AFTER SWAP****
 First number = 31.6
 Second number = 9.0
Click Here to View the Explanation!
  • This program involves the swapping of the numbers without the usage of any temporary variable. It therefore takes place by using simple numerical techniques.
  • Initially, an operation firstNum secondNum is stored into the variable firstNum.

firstNum = firstNum – secondNum

Considering the values, 9.0 and 31.6, the variable will have the following value:

firstNum = 9.0 – 31.6

  • The value of the variable secondNum is then added into the previously calculated value of firstNum and the calculation is stored into the variable secondNum. This is done for performing the process of swapping. It will be as follows:

secondNum = firstNum + secondNum

secondNum = (9.0 – 31.6) + 31.6= 9.0

  • Now, the variable secondNum holds the value 9.0 which was initially the value of firstNum. For storing the value of secondNum into firstNum, the last step of swapping will be to subtract the calculated value of firstNum from the above calculated value of secondNum i.e. 9.0. This will result in the required swapping. It will be as follows:

firstNum = secondNum – firstNum

firstNum = 9.0 – (9.0 – 31.6) = 31.6

  • The numbers are swapped using the above mathematical procedure and the results will be displayed using println().

%d bloggers like this: