Java Program to Swap Two Numbers

Swapping two numbers in Java programming means swapping the values of two variables, which can be done using a temporary variable or without using a temporary variable. Swapping values is useful in programming, such as sorting or reordering elements in an array.

Example:
There are two variables m & n. Value of m is “4” & value of n is “5”.
Before Swapping: m value = 4; n value = 5
After Swapping: m value = 5; n value = 4

Problem Description

Write a Java Program to Read Two Integers M and N & Swap their Values.

Problem Solution

1. Enter any two integer numbers as input.
2. After that we take a new variable temp and copy first variable to this temp variable.
3. Now we copy second variable to first variable and temp variable into second variable.
4. Hence we get the swapped values as output.

Let’s discuss different ways to swap two numbers in Java language.

Method 1: Swap Two Numbers in Java using Temporary Variable

In this method, we will use another temporary variable to help in the swapping of the two numbers.

advertisement
advertisement
Program/Source Code

Here is the source code of the Java Program to Read Two Integers M and N & Swap their Values using temp variable. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
* Java program to read two integers M and N and to swap their values.
* Using Temporary variables.
*/
 
import java.util.Scanner;
public class Swap_Integers 
{
    public static void main(String args[])
    {
        int m, n, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number:");
        m = s.nextInt();
        System.out.print("Enter the second number:");
        n = s.nextInt();
        temp = m;
        m = n;
        n = temp;
        System.out.println("After Swapping");
        System.out.println("First number:"+m);
        System.out.println("Second number:"+n);
    }
}
Program Explanation

1. The class “Swap_Integers” is declared as public and contains the main() method.
2. The main() method declares three integer variables: m, n, and temp.
3. The user is prompted to enter the first and second numbers using the println() and nextInt() methods respectively.
4. The value of “m” is assigned to “temp“, “n” is assigned to “m“, and “temp” is assigned to “n“, thus swapping the values of m and n.
5. The swapped values of m and n are printed using the println() method.

Time Complexity: O(1)
The time complexity of this program is O(1) since the number of operations performed is constant and does not depend on the size of the input.

Space Complexity: O(1)
The space complexity of this program is O(1) since only a constant amount of space is used to store the variables and the Scanner object.

Run Time Testcases
 
$ javac Swap_Integers.java
$ java Swap_Integers
 
Enter the first number:5
Enter the second number:7
After Swapping
First number:7
Second number:5

Method 2: Swap Two Numbers in Java without using any Temporary Variable

In this approach, we will swap two integers without using any temporary variables.

advertisement
Program/Source Code

Here is source code of the Java program that will swap two numbers without using any temporary variables. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
* Swap Two Numbers in Java without using any Temporary Variable
*/
import java.util.Scanner;
 
public class Swap_Integers
{
    public static void main(String args[])
    {
        int m, n;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number:");
        m = s.nextInt();
        System.out.print("Enter the second number:");
        n = s.nextInt();
        m = m + n;
        n = m - n;
        m = m - n;
        System.out.println("After Swapping");
        System.out.println("First number:"+m);
        System.out.println("Second number:"+n);
   }
}
Program Explanation

1. The program starts by importing the Scanner class from the java.util package.
2. The class “Swap_Integers” is declared as public and contains the main() method.
3. The main() method declares two integer variables: m and n. m and n are the two numbers to be swapped.
4. The user is prompted to enter the first and second numbers using the println() and nextInt() methods respectively.
5. The values of m and n are swapped without using a temporary variable.
6. The swapped values of m and n are printed using the println() method.

Time Complexity: O(1)
The time complexity of the above program is constant time or O(1), as the number of operations performed does not change with the size of the input.

advertisement

Space Complexity: O(1)
The space complexity of the program is O(1), as it only uses a constant amount of memory to store the input variables and the Scanner object.

Program Output:
 
$ javac Swap_Integers.java
$ java Swap_Integers
 
Enter the first number: 4
Enter the second number: 5
After Swapping
First number: 5
Second number:4

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.