Java Program to Swap values using third variable

In Java, to swap two values using a third variable, we should know the concepts of operators and how to assign values. In this article we will learn swapping values using a third variable in java through an example, detailed logic and program explanation for better understanding.

For example, if the two input numbers are a=12 and b=13, the resulting output should be b=12 and a=13.

Required Knowledge

Problem Statement

We need to write a Java program to swap two values using third variable. For two input integer variables, the program should swap (exchange) the values in those variables using third variable.

Let’s see an example,

Example:

For two input variables a =67 and b = 23, Output should be a =23 and b=67

Explanation:

Input integer number a= 67

Input integer number b= 23

Take third variable c

Swapping: c=a, c=67
a=b, a=23
b=c, b=67

Now value in a=23 and b=67

Therefore, the values in a and b are swapped.

Logic to swap two values using third variable

  • Take two input integer values from the user and store them in variable a and b respectively. Define variable c.
  • c=a
    • This line assigns the value of ‘a’ to ‘c’. Now, ‘c’ holds the value of ‘a’.
  • a=b
    • This line assigns the value of ‘b’ to ‘a’. Now, ‘a’ holds the value of ‘b’.
  • b=c
    • This line assigns the value of c (which is the original value of a) to b. Now, b holds the original value of a.
  • After swapping, the program will print the result.

Java program to swap two Values using third variable

In the below program, we are swapping two values using the third variable in Java.

import java.util.Scanner;
public class SwapTwoValues {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
	  System.out.println("Enter integer number a :");
	  int a = s.nextInt();
	  System.out.println("Enter integer number b :");
	  int b = s.nextInt();
	  int c;
		System.out.println("Before Swapping: "); 
		System.out.println("a: "+a);
		System.out.println("b: "+b);
		c=a; //a=60, stores in c
		a=b;// a=40, stored b value in a
		b=c;//b=60, stores c value in b
		System.out.println("After Swapping");
		System.out.println("a: "+a);
		System.out.println("b: "+b);
		s.close();
	}
}

Output:

Enter integer number a :
123
Enter integer number b :
125
Before Swapping:
a: 123
b: 125
After Swapping
a: 125
b: 123

Java program explanation to swap values using third variable

  1. The above java program imports the Scanner class for user input.
  2. It defines a class named SwapTwoValues.
  3. In the main method:
    • It creates a Scanner object to read user input.
    • Asks the user to enter two numbers.
    • Reads and stores the two numbers in variables a and b.
    • Declares an integer variable c.
    • Prints the values of a and b before swapping.
    • Swaps the values of a and b using a third variable c.
    • Prints the values of a and b after swapping.
    • Closes the Scanner object.

Conclusion

In this article, we have learnt how to swap two values using third variable in Java. Here, we understood, how to use operators for swapping and assigning values to the variables to do swapping and seen the program. Hope you understand the logic and program to do swapping.

You can also check our another awesome Java tutorials

Happy Java Coding!!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.