Java Program To Swap Two Numbers WithOut Temporary Variable
Chapter:
Interview Programs
Last Updated:
20-07-2016 03:06:47 UTC
Program:
/* ............... START ............... */
public class JavaSwapNumbers {
public static void main(String a[]) {
int x = 30;
int y = 50;
System.out.println("Before swap:");
System.out.println("x value: " + x);
System.out.println("y value: " + y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swap:");
System.out.println("x value: " + x);
System.out.println("y value: " + y);
}
}
/* ............... END ............... */
Output
Before swap:
x value: 30
y value: 50
After swap:
x value: 50
y value: 30
Tags
Swap Two Numbers WithOut Temporary Variable, Java