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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad