Program to input two variables and swap them without using third variable JAVA


Write a Program to input two variables and swap them without using third variable.




import java.util.*;
class swap
{
    public static void main(String args[])
    {
      Scanner sc = new Scanner(System.in);
      int a = sc.nextInt();
      int b = sc.nextInt();
      System.out.println("Before Swapping: " + " " +"a ="  + a + " b = " + b);
      a = a+ b;
      b = a - b;
      a = a- b;
      System.out.println("a = " + a + ", b = " + b);
    }   
}


     
                     

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com