FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Swap Two Numbers With and Without Third Variable In Java

Swap Two Numbers With and Without Third Variable In Java


// with third variable

class SwapTwoNum{
    public static void main(String args[]){
        
	int x = 34;
	int y = 50;
	
	int temp = x;
	x = y;
	y = temp;

	System.out.printf("X is : %d, Y is : %d",x,y);	
	
    }
}


// Without Third Variable

class SwapTwoNumWithout{
    public static void main(String args[]){
        
	int x = 34;
	int y = 50;
	
	x = x+y;
        y = x-y;
        x = x-y;
	

	System.out.printf("X is : %d, Y is : %d",x,y);	
	
    }
}