Swap Logic 1 in Java

Table of Contents

Swap Logic 1 in Java

Compile and Run the following code with Command-Prompt Or download Notepad++

For Compilation javac Sample.java

For Execution java Sample

import java.util.Scanner;
class Sample
{
  public static void main(String[] args)
  {
    int x,y,temp;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the value of x and y");
    x = sc.nextInt();
    y = sc.nextInt();
    System.out.println("Before swapping x= "+x+"and y ="+y);
    temp = x;
    x = y;
    y =temp;
     System.out.println("After swapping x= "+x+"and y ="+y); 
  }
}

Leave a Reply