Write a program to swap the value of 2 variables without using 3rd variable


Write a program to swap the value of 2 variables without using 3rd variable




import java.util.Scanner;

class SwapTwoValues

{

public static void main(String[] args) 

{

        int a,b;

        Scanner scr=new Scanner(System.in);

        System.out.println("Enter two values ");

        a=scr.nextInt();

        b=scr.nextInt();

        System.out.println("Befor swapping a = "+a+" and b = "+b);

        // swap the values of a and b a=10 b=20

        a=a+b;

        b=a-b;

        a=a-b;

        System.out.println("After swapping a = "+a+" and b = "+b);

        

    }

}

Output




Post a Comment

0 Comments