1.

Write A Program To Swap Two Numbers Without Using Any Temp Variable?

Answer»

Program to swap two numbers without using any TEMP variable is as below:

public class swap{

public static VOID MAIN (String ARGS[])

{

int x = 20;

int y =30;

System.out.println(“Numbers before swapping”);

System.out.println(“ number x is “ + x);

System.out.println(“number y is “ +y);

// Swapping numbers

x= x+y;

y=x-y;

x=x-y;

System.out.println(“Numbers after swapping”);

System.out.println(“ number x is “ + x);

System.out.println(“number y is “ +y);

}

}

Program to swap two numbers without using any temp variable is as below:

public class swap{

public static void main (String args[])

{

int x = 20;

int y =30;

System.out.println(“Numbers before swapping”);

System.out.println(“ number x is “ + x);

System.out.println(“number y is “ +y);

// Swapping numbers

x= x+y;

y=x-y;

x=x-y;

System.out.println(“Numbers after swapping”);

System.out.println(“ number x is “ + x);

System.out.println(“number y is “ +y);

}

}



Discussion

No Comment Found