How to Swap Numbers Without Using Third Variable in Java


In this example, you’ll learn how to swap value of two numbers without using third variable in Java.

Source Code

package com.beginner.examples;

public class SwapNumExample {

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

Output:

m = 10 and n = 2019
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments