How to Swap Number Without Using Third Variable in Java


In this example we will show the method to swap number without using third variable in Java.

Source Code

package com.beginner.examples;

public class SwapNum {

  public static void main(String[] args){
    int m = 2020;
    int n = 6;
    //swap the value
    m = m + n;
    n = m - n;
    m = m - n;
    System.out.println("m = " + m + " and n = " + n);
  }
}

Output:

m = 6 and n = 2020
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments