Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Using extra space
3.
Without using extra space
4.
Frequently Asked Questions
4.1.
Can swapping two numbers be performed without using user input?
4.2.
Can swapping two numbers be performed when the numbers are float or double?
4.3.
How can we take input in the program if we want to?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Swapping of Two Numbers

Author Aditi
1 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The swapping of two numbers is an essential program used in different algorithms. Swapping means exchanging two variables. The first thing needed to write a program for swapping two numbers is to take two numbers and then switch the values. It can be done in two ways, i.e., using extra space and without extra space. This blog will help you explain all the methods to write the program for swapping two numbers with the help of code.

Using extra space

With the extra space, we can easily swap two numbers. Variable num1, num2, temp act as containers where we can store numbers. The following steps are used in the program for swapping.

Two numbers will be stored in the variable num1 and num2. Now for swapping two numbers, first, we hold the num1 value in temp. In the temp variable, a copy will be saved. Now, store the value of num2 in the num1 variable. At last, copy the value stored in the temp variable to the num1 variable.

public class swap {
    public static void main(String[] args) {
        //Program for swapping two numbers
        int num1 = 4;
        int num2 = 5;

        //Before Swapping
        System.out.println("~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~");
        System.out.println("Number 1 : "+ num1);
        System.out.println("Number 2 : "+ num2);
        System.out.println();

        int temp = num1;
        num1 = num2;
        num2 = temp;

        //After Swapping
        System.out.println("~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~");
        System.out.println("Number 1 : "+ num1);
        System.out.println("Number 2 : "+ num2);
       
        scn.close();
    }
}

Output :

~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~
Number 1 : 4
Number 2 : 5

~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~
Number 1 : 5
Number 2 : 4

Numbers 4 and 5 are numbers stored in the variables num1 and num2. Then, swapping two numbers is shown in the above output by following the steps.

Try it by yourself on Java Online Compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Without using extra space

We can perform swapping two numbers without using extra space. It will be better than the previous program because we will save space here. The code is written below.

public class swap {
    public static void main(String[] args) {
        //Program for swapping two numbers
        int num1 = 10;
        int num2 = 20;

        //Before Swapping
        System.out.println("~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~");
        System.out.println("Number 1 : "+ num1);
        System.out.println("Number 2 : "+ num2);
        System.out.println();

        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;

        //After Swapping
        System.out.println("~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~");
        System.out.println("Number 1 : "+ num1);
        System.out.println("Number 2 : "+ num2);
       
        scn.close();
    }
}

Output :

~~~~~~~~~ Before Swapping of Numbers ~~~~~~~~~
Number 1 : 10
Number 2 : 20

~~~~~~~~~ After Swapping of Numbers ~~~~~~~~~
Number 1 : 20
Number 2 : 10

Numbers 10 and 20 are stored in num1 and num2, respectively. Now, 

Step 1: num1 = num1+num2 , so, num1 will now become 30(10+20)

Step 2: num2 = num1 - num2 , so, num2 = 30 - 20 = 10. It means now num2 will have value 10.

Step 3: num1 = num1 - num2 , so, num1 = 30 - 10 = 20. It means the new value of num1 is 20.

By following the above steps, swapping two numbers is done. The new value of num1 is 20, and num2 is 10.

Also check out Addition of Two Numbers in Java here.

Also see, Duck Number in Java

Frequently Asked Questions

Can swapping two numbers be performed without using user input?

Yes, you have to initialize the variable with some value instead of using a scanner object to input from the user.

Can swapping two numbers be performed when the numbers are float or double?

Yes, swapping can be done even when the numbers are of float or double type.

How can we take input in the program if we want to?

Scanner is a class in Java that is used to take input from the user. It can input primitive data types like int, float, string, or double.

Also Read - Strong number in c

Conclusion

In this article, we have extensively discussed the swapping of two numbers and its implementation in Java. Swapping two numbers is a simple program where two numbers are exchanged. It can be done in two ways, using extra space and without using extra space. It will perform more calculations without using additional variables than when extra space is used. But both are simple to apply.

We hope that this blog has helped you enhance your knowledge regarding swapping of two numbers and if you would like to learn more, check out our articles on Swapping of two numbers in C++. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Reverse a String in Java
Next article
Int to String Conversion in Java
Live masterclass