Home Show/Hide Menu
Bluej Programs
➜ Test-paper-IT-10
➜ Suggestion Paper, Class-12

bluej Programs

2.Write a program to swap two values without using 3rd variable

import java.util.*;
class swapping
{
    void main()
    {
        Scanner sc=new Scanner(System.in);
        int x,y;
        System.out.println("Enter two values:");
        x=sc.nextInt();
        y=sc.nextInt();
        System.out.println("Before Swapping \nx:"+x+"\ny:"+y);
        x=x+y;
        y=x-y;
        x=x-y;
        System.out.println("After Swapping \nx:"+x+"\ny:"+y);
    }
}


Output:
Enter two values:
23
45
Before Swapping 
x:23
y:45
After Swapping 
x:45
y:23
 
ADVERTISEMENT