by Dinesh Thakur Category: Data Type

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)
          System.out.println("Enter the First and Second Number ");
          Scanner scan = new Scanner(;
          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){}

Swap Numbers Without Using Third Variable Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.