java swapping program,swapping program in java, java programs

import java.util.*; 
class Swap_With 
public static void main(String[] args) 
{
int x, y, t;// x and y are to swap 
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 :
Enter the value of X and Y
2
3
before swapping numbers: 2 3 
After swapping: 3 2

Post a Comment

Previous Post Next Post