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

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024
In this Java tutorial, we will learn how to add two integers in JAVA and storing the integer in an...
  • Java
  • December 2, 2024