How to Swap Two Numbers In Java

How to swap two programs in java

Java Operators 

  • Java operators are symbols that are used to perform operations on variables and values. For example, the plus symbol (+) is used to add two values together.
  • Java operators can be classified into several categories, such as arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, and more.
    • You must be familiar with following topics to understand the correspond example Such as: Java Data Types  and Java Operators.
  • To understand the how to Swap Two Numbers In Java, Read the Complete Article.

Steps to Swap two numbers In Java Program:

  • Here are the Steps to swap two numbers in Java, You can follow these steps:
    • Declare two variables, say a and b, and initialize them with the values to be swapped.
    • Declare a temporary variable, say temp, and assign the value of a to it.
    • Assign the value of b to a.
    • Assign the value of temp to b.
    • The values of a and b should now be swapped.

Let’s look at the Java Program to swap two numbers to perform certain operations.

Example 1: Java Program to Swap Two numbers.

Run
public class Main {
    public static void main(String[] args) {
        int num1 = 5;
        int num2 = 10;
        int temp;

        System.out.println("Before swapping:");
        System.out.println("Num1 = " + num1);
        System.out.println("Num2 = " + num2);

        // swap the numbers using a temporary variable
        temp = num1;
        num1 = num2;
        num2 = temp;

        System.out.println("After swapping:");
        System.out.println("Num1 = " + num1);
        System.out.println("Num2 = " + num2);
    }
}

Output

Before swapping:
Num1 = 5
Num2 = 10
After swapping:
Num1 = 10
Num2 = 5

Example 2 :Java Program to Swap two Numbers.

Run

public class Main {
    public static void main(String[] args) {
        int num1 = 50;
        int num2 = 90;

        System.out.println("Before swapping:");
        System.out.println("Num1 = " + num1);
        System.out.println("Num2 = " + num2);

        // swap the numbers without using a temporary variable
        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;

        System.out.println("After swapping:");
        System.out.println("Num1 = " + num1);
        System.out.println("Num2 = " + num2);
    }
}

Output

Before swapping:
Num1 = 50
Num2 = 90
After swapping:
Num1 = 90
Num2 = 50

Example 3: Java Program to swap two numbers 

Run
public class Main
{

  public static void main (String[]args)
  {

    int num1 = 7878;

    int num2 = 8787;


      System.out.println ("Before swapping:");

      System.out.println ("Num1 = " + num1);

      System.out.println ("Num2 = " + num2);


    // swap the numbers using a temporary variable
      num1 = num1 ^ num2;

      num2 = num1 ^ num2;

      num1 = num1 ^ num2;


      System.out.println ("After swapping:");

      System.out.println ("Num1 = " + num1);

      System.out.println ("Num2 = " + num2);

  }
}

Output

Before swapping:
Num1 = 7878
Num2 = 8787
After swapping:
Num1 = 8787
Num2 = 7878

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription