Tutorials Hut

Java Program To Swap Two Values

 

import java.util.*;  
class SwapTheValues { 
   public static void main(String[] args) { 
    // x, y and temp variables
      int x, y, t;
      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 numbers: "+x +"  "+ y);  

      /*swapping */  
      t = x;  
      x = y;  
      y = t;  
      System.out.println("After swapping: "+x +"   " + y);  
      System.out.println( );  
    }    


Output:

before swapping numbers: 10 12

After swapping: 12 10

 

 


















  • Leave a Reply

    Your email address will not be published. Required fields are marked *