Programming Examples
Swapping of two variables without using a third variable
Create a program that will swap the values stored in two variables without using a third variable.
Solution
public class Swaping
{
public static void main(String args[])
{
int a=40,b=30;
System.out.println("Before Swap the value of a="+a+" and b="+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After Swap the value of a="+a+" and b="+b);
}
}
Output
Before Swap the value of a=40 and b=30
After Swap the value of a=30 and b=40