Program Swapping Two Numbers In Java Without 3rd Variable - HelpMeOutAlways

Saturday, February 23, 2019

Program Swapping Two Numbers In Java Without 3rd Variable

Source Code

import java.util.Scanner;
class InterChangeNo{
    
    public void swap(){
        int a, b;
      System.out.println("Enter the value of a and b:");
      Scanner in = new Scanner(System.in);
 
      a = in.nextInt();
      b = in.nextInt();
 
      System.out.println("Before Swapping\nx = "+a+"\ny = "+b);
 
      a = a + b;
      b = a - b;
      a = a - b;
 
      System.out.println("After Swapping\nx = "+a+"\ny = "+b);
    }
    
}
 
class Main
{
   public static void main(String args[])
   {
      InterChangeNo obj=new InterChangeNo();
      obj.swap();
   }
}

Output

Java program

No comments:

@way2themes