This Swap Numbers Without Using Third Variable Java Example shows how to exchange numbers without using third variable using java. i.e. if the input is n1 100 and n2 200 then after swaping output will be n1 is 200 and n2 is 100. You should not use any temporary variable to swap the numbers.
Here is the Java Example for the program SwapNumberWithoutThirdVariableExample :
import java.util.Scanner; public class SwapNumberWithoutThirdVariableExample { public static void main(String[] args) { try { System.out.println("Enter the First and Second Number "); Scanner scan = new Scanner(System.in); int n1 = scan.nextInt(); int n2 = scan.nextInt(); System.out.println("Before Swapping number1 = " + n1 + " number2= " +n2); n1 = n1 + n2; n2 = n1 - n2; n1 = n1 - n2; System.out.println(" After Swapping number1 = " + n1 + " number2= " +n2); } catch (Exception e){} } }