Friday, January 23, 2015

Java fresher interview question:Swap of values without using 3rd variable

import java.util.Scanner;
public class Swappingwithout {
    public static void main(String[] args)
    {
        Scanner Sc=new Scanner(System.in);
        int X,Y;
        System.out.println("Please enter the value of X");
        X=Sc.nextInt();
        System.out.println("The value of X is:"+X);
        System.out.println("Please enter the value of Y");
        Y=Sc.nextInt();
        System.out.println("The value of Y is:"+Y);
        // logic Starts
        Y=X+Y;
        X=Y-X;
        System.out.println("After Swapping the value of X is:"+X);
        Y=Y-X;
        System.out.println("After Swapping the value of Y is:"+Y);

    }

}

No comments: