Signup/Sign In

Java Program to Swap Two Numbers

In this tutorial, we will learn how to swap two variables in java. Swapping two variables means interchanging the values of both the variables with each other. For example, if variable A contains X value and variable B contains a value, then after swapping A contains Y value and B contains X value. But before moving forward if you are not familiar with the concept of variables in Java, then do check the article on Variables in Java.

Input: First Variable: 3

Second Variable: 6

Output: First Variable: 6

Second Variable: 3

Program 1: Swap Two Numbers in Java

In this program, we will see how to swap two numbers by using a third variable.

Algorithm

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare two variables.

  4. Ask the user to initialize the variables.

  5. Print the values of both the variables before swapping.

  6. Declare a temporary variable.

  7. Assign the value of the first variable to the temporary variable.

  8. Now, assign the value of the second variable to the first variable.

  9. Assign the value of the temporary variable which contains the value of the first variable to the second variable.

  10. Print the values of both variables.

  11. Stop.

Below is the code example to swap two value in Java.

//Java Program to Swap two numbers using a temporary variable
import java.util.*; 
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first number");
        int num1=sc.nextInt();
        System.out.println("Enter the second number");
        int num2=sc.nextInt();
        System.out.println("Before Swapping numbers are: ");
        System.out.println("The first Number is "+num1);
        System.out.println("The second Number is "+num2);
        //Use a temporary variable to swap the numbers
        int temp=num1;
        num1=num2;
        num2=temp;
        System.out.println("After Swapping numbers are: ");
        System.out.println("The first Number is "+num1);
        System.out.println("The second Number is "+num2);
     }
}


Enter the first number 3
Enter the second number 5
Before Swapping numbers are:
The first Number is 3
The second Number is 5
After Swapping numbers are:
The first Number is 5
The second Number is 3

Program 2: Swap Two Numbers in Java

In this program, we will see how to swap two numbers without using a third variable.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare two variables.

  4. Ask the user to initialize the variables.

  5. Print the values of both the variables before swapping.

  6. Subtract the second variable from the first variable and assign that value to the first variable.

  7. Add the value of both the variables and assign it to the second variable.

  8. Subtract the value of the first variable from the second variable and assign that value to the first variable.

  9. Print the values of both variables.

  10. Stop

Below is the code example to swap two value in Java.

//Java Program to Swap two numbers without using a temporary variable
import java.util.*; 
public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first number");
        int num1=sc.nextInt();
        System.out.println("Enter the second number");
        int num2=sc.nextInt();
        System.out.println("Before Swapping numbers are: ");
        System.out.println("First Number is "+num1);
        System.out.println("Second Number is "+num2);
        num1=num1-num2;
        num2=num1+num2;
        num1=num2-num1;
        System.out.println("After Swapping numbers are: ");
        System.out.println("First Number is "+num1);
        System.out.println("Second Number is "+num2);
     }
}


Enter the first number 8
Enter the second number 9
Before Swapping numbers are:
The first Number is 8
The second Number is 9
After Swapping numbers are:
The first Number is 9
The second Number is 8



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.