Friday, January 23, 2015

Java fresher interview question: Swapping of 2 numbers using 3rd variable

import java.util.Scanner;
public class Swapping
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int x,y,temp;
        System.out.println("Please Enter the value of x:");
        x=sc.nextInt();
        System.out.println("The value of x :"+x);
        System.out.println("Please Enter the value of y:");
        y=sc.nextInt();
        System.out.println("The value of y :"+y);
        temp=x;
        x=y;
        y=temp;
        // after Swapping
        System.out.println("After Swapping,the value of x:" +x);
        System.out.println("After Swapping,the value of y:" +y);
    }
}

No comments: