Sunday, November 19, 2017

How to swap two numbers without using temporary variable?

Swap two numbers without using temp variable?


package com.coding.core;

public class SwapTwoNumbers {

public static void main(String[] args) {

// TODO Auto-generated method stub
int x = 10;
int y = 20;
System.out.println("Before swap:");
System.out.println("x value: " + x);
System.out.println("y value: " + y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swap:");
System.out.println("x value: " + x);
System.out.println("y value: " + y);
}


}



No comments:

Post a Comment

How to remove duplicate elements from an ArrayList in Java

How to remove duplicate elements from an ArrayList in Java. Ø   ArrayList allows adding duplicate elements, sometime there may be...