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

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad