w3resource

Java: Swap two variables

Java Basic: Exercise-15 with Solution

Write a Java program to swap two variables.

Java: Swapping two variables

Swapping two variables refers to mutually exchanging the values of the variables. Generally, this is done with the data in memory.

The simplest method to swap two variables is to use a third temporary variable :

define swap(a, b)
    temp := a
    a := b
    b := temp

Pictorial Presentation:

Java: swap two variables

Sample Solution-1

Java Code:

public class Exercise15 {
 
 public static void main(String[] args) {
   // Declare variables for the values to be swapped
   int a, b, temp;
   
   // Assign values to variables a and b
   a = 15;
   b = 27;
   
   // Print the values before swapping
   System.out.println("Before swapping : a, b = " + a + ", " + b);
   
   // Perform the swap using a temporary variable
   temp = a;
   a = b;
   b = temp;   
   
   // Print the values after swapping
   System.out.println("After swapping : a, b = " + a + ", " + b);
 }
}

Explanation:

In the exercise above -

  • Initialize two integer variables, 'a' with the value 15 and 'b' with the value 27.
  • Prints the original values of 'a' and 'b' before swapping.
  • Use a temporary variable temp to temporarily store the value of 'a'.
  • Assign the value of 'b' to 'a'.
  • Assign the value stored in temp (originally from a) to 'b'.
  • Finally, it prints the values of 'a' and 'b' after the swapping operation.

Without using third variable.

Sample Solution-2

Java Code:

public class Exercise15 {
  public static void main(String[] args) {
     // Declare and initialize integer variables a and b
     int a, b;
     a = 15;
     b = 27;
     
     // Print the values before swapping
     System.out.println("Before swapping : a, b = " + a + ", " + b);
     
     // Perform the swap without using a temporary variable
     a = a + b;  // Add a and b and store the result in a
     b = a - b;  // Subtract the original b from the new a and store the result in b
     a = a - b;  // Subtract the new b from the updated a and store the result in a
     
     // Print the values after swapping
     System.out.println("After swapping : a, b = " + a + ", " + b);
  }
}

Explanation:

In the exercise above -

  • Initialize two integer variables, 'a' with the value 15 and 'b' with the value 27.
  • Prints the original values of 'a' and 'b' before swapping.
  • Use arithmetic operations to swap values without using a temporary variable:
    • a = a + b adds 'a' and 'b' and stores the sum in 'a'. Now, 'a' contains the sum of the original 'a' and 'b'.
    • b = a - b subtracts the original 'b' from the updated 'a' and stores the result in 'b'. Now, 'b' contains the original value of 'a'.
    • a = a - b subtracts the updated 'b' (which now contains the original value of 'a') from the updated 'a' and stores the result in 'a'. Now, 'a' contains the original value of 'b'.
  • Finally, it prints the values of 'a' and 'b' after the swapping operation.

Sample Output:

Before swapping : a, b = 15, 27                                                                               
After swapping : a, b = 27, 15

Flowchart:

Flowchart: Java exercises: Swap two variables

Sample Solution (Input from the user):

Java Code:

import java.util.Scanner;

public class Main {
 public static void main(String[] args) {
   // Declare integer variables for storing user input and swapping
   int x, y, z;
   
   // Create a Scanner object to read input from the user
   Scanner in = new Scanner(System.in);

   // Prompt the user to input the first number
   System.out.println("Input the first number: ");
   x = in.nextInt();
   
   // Prompt the user to input the second number
   System.out.println("Input the second number: ");
   y = in.nextInt();

   // Perform the swap using a temporary variable 'z'
   z = x;
   x = y;
   y = z;

   // Display the swapped values
   System.out.println("Swapped values are: " + x + " and " + y);
  }
}

Sample Output:

Input the first number: 
 36
Input the second number: 
 44
 Swapped values are:44 and 36

Flowchart:

Flowchart: Java exercises: Swap two variables

Java Code Editor:

Previous: Write a Java program to print an American flag on the screen.
Next: Write a Java program to print a face.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/basic/java-basic-exercise-15.php