Showing posts with label swap without using thord variable. Show all posts
Showing posts with label swap without using thord variable. Show all posts

Friday 3 January 2014

Write a java program for swapping of two numbers

Write a java program for swapping of two numbers.

package programs;

import java.util.Scanner;

public class Swapping {
public static void main(String[] args)  {

int x, y;
     System.out.println("Enter x and y");
     Scanner in = new Scanner(System.in);
     x = in.nextInt();
     y = in.nextInt();  
     System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
     x = x+ y; 
     y = x -y; 
     x = x -y;
     
     System.out.println("After Swapping\nx = "+x+"\ny = "+y);
  }
}