Java Program to Swap Two Numbers

The Java program swaps two numbers. There are two approaches to achieve this functionality, the first is using a temporary variable and the other is using (arithmetic operator ) without using a temporary variable.

Swap two numbers using a temporary variable

Here’s is an example of swapping two numbers with the help of a temporary variable. It is also possible to interchange two numbers without using a temporary variable which we’ll be discussing later.

Approach :

  • Store a value of the first number to a temporary variable.
  • Then, assign the value of the second number to the first number.
  • Lastly, put the value held by the temporary variable (actually, the value of the first number) to the second number.

swap two numbers using temprory variable

Program:

/*
*
Swapping numbers program using temporary variable in Java
*
*/
import java.util.Scanner;
public class Swapwithtemp {

public static void main(String[] args) {
int number, number1,temp;
Scanner ed = new Scanner(System.in);
System.out.print("\n Enter two numbers:\n First number = ");
number = ed.nextInt();
System.out.print("\n Second number = ");
number1=ed.nextInt();
System.out.print("\n\n ****** Before Swapping *******\n");
System.out.print("\n First number = "+number+" \n Second number ="+number1);

temp=number;
number=number1;
number1=temp;
System.out.print("\n ********* After Swapping *****\n");
System.out.println("\n First number = "+number+" \n Second number ="+number1);
}

}

Output:

swapping in java

Swap two numbers without using temporary variable

We saw swapping two numbers using a temporary variable. But we can also do it without the help of any third one.

In this approach, arithmetic operators are used which can be a combination of addition & subtraction and multiplication & division. However, we’ll go with addition & subtraction.

Approach :

  • Store the sum of two numbers to any of the given variables (suppose first variable ).
  • Then, subtract another number from the sum and assign them to another variable (suppose the second variable ).
  • Subtract again and this time the outcome of the second would be subtracted from the sum, and store it to the first variable.

swap to two numbers without temp

Program:

/*
*
Swapping numbers program without using temporary variable in Java
*
*/

import java.util.Scanner;

public class Swapwithouttemp {

public static void main(String[] args) {
int number, number1;
Scanner ed = new Scanner(System.in);
System.out.print("\n Enter two numbers:\n First number = ");
number = ed.nextInt();
System.out.print("\n Second number = ");
number1=ed.nextInt();
System.out.print("\n\n ****** Before Swapping *******\n");
System.out.print("\n First number = "+number+" \n Second number ="+number1);
number = number +number1;
number1=number-number1;
number=number-number1;
System.out.print("\n ********* After Swapping *****\n");
System.out.println("\n First number = "+number+" \n Second number ="+number1);

}

}

Output:

swap java

Leave a Reply