Sample Programs in java

How To Swap Numbers Using Temp Variable

Java Example Program to swap numbers without using third or temp variable

[java]
/* java program for swapping of two numbers without using third variable*/
/**
*@author:Candidjava.com
*@Descripton:swapping of two numbers without third variable
*/
public class Swap {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(“Before swapping:”); // display message
System.out.println(a);
System.out.println(b);
a = a + b;
b = a – b;
a = a – b;
System.out.println(“After Swapping:”);
System.out.println(a);
System.out.println(b);
}
}
[/java]

Output:

Before swapping:
a=10
b=5
After swapping:
a=5
b=10