Java Program to Swap Two Numbers

In this Java Program, you’ll learn how to swap two numbers using the Java programming language

How to Swap Two numbers in JAVA? 

Example 1: Swap two Numbers using a variable. 

RUN CODE SNIPPET
public class Main  

{ 

    public static void main(String[] args) 

     { 

        int first = 10, second = 25; 

        System.out.println("*****Before swap*****"); 

        System.out.println("First number = " + first); 

        System.out.println("Second number = " + second); 

        int a = first; 

        first = second; 

        second = a; 

        System.out.println("*****After swap*****"); 

        System.out.println("First number = " + first); 

        System.out.println("Second number = " + second); 

    } 

}

OUTPUT 

*****Before swap***** 

First number = 10 

Second number = 25 

*****After swap***** 

First number = 25 

Second number = 10

In the above program, the two numbers 10 and 25 which are going to be swapped are stored in a variable first and second respectively. 

First, the value 10 is stored in a variable “a”, and the variable 25 is stored in a variable first. 

Finally, the value of the “a” variable is stored in variable second. 

This is how swapping of two variables occurs, print the result using the println() statement. 

Here the use of variable “a” is only to hold the value of the first variable before swapping. 

Example 2: Swapping Two numbers without variable 

RUN CODE SNIPPET
public class Main 

 { 

    public static void main(String[] args) { 

        int first = 10, second = 25; 

        System.out.println("*****Before swap*****"); 

        System.out.println("First number = " + first); 

        System.out.println("Second number = " + second); 

        first = first - second; 

        second = first + second; 

        first = second - first; 

        System.out.println("*****After swap*****"); 

        System.out.println("First number = " + first); 

        System.out.println("Second number = " + second); 

    } 

}

OUTPUT 

*****Before swap***** 

First number = 10 

Second number = 25 

*****After swap***** 

First number = 25 

Second number = 10

In the above program, we have not used a variable to swap two numbers. 

In the first variable, we are going to subtract the second variable value. Then in the second variable, we are going to add the first variable value, which holds the initial value of the first variable. 

Here the swapping of two numbers happens, again in the first variable, we are going to subtract the swapped second variables value to the calculated first variable value. 

Finally, print the values using the println() statement

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022