Java program to swap two variables without using third variable.

SWAPPING WITHOUT THIRD VARIABLE.




JAVA Code :-



import java.util.Scanner;

public class swap


{


static void swapping(int x,int y)


{


  System.out.println("**Before swapping**\n1st value = "+x+"\n2nd value = "+y);


  x=x+y;


  y=x-y;


  x=x-y;


  System.out.println("**After swapping**\n1st value = "+x+"\n2nd value = "+y);


}


public static void main(String[] args)


{


int m,n;


Scanner sc=new Scanner(System.in);


System.out.print("Enter 1st value:");


m=sc.nextInt();


System.out.print("Enter 2nd value:");


n=sc.nextInt();


sc.close();


swapping(m,n);


}


}




 INPUT && OUTPUT :)



Post a Comment

0 Comments