Advertisement
brilliant_moves

Swap2.java

Jun 14th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.61 KB | None | 0 0
  1. import java.util.Scanner; // for user input
  2.  
  3. public class Swap2 {
  4.  
  5.     /**
  6.     *   Program :   Swap2.java
  7.     *   Purpose :   Swap two numbers, without using a third variable
  8.     *   Creator :   Chris Clarke
  9.     *   Created :   14.06.2013
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         int x, y;
  15.  
  16.         Scanner scan = new Scanner(System.in);
  17.  
  18.         System.out.print("Enter value for x: ");
  19.         x = scan.nextInt();
  20.  
  21.         System.out.print("Enter value for y: ");
  22.         y = scan.nextInt();
  23.  
  24.         x += y;
  25.         y  = x-y;
  26.         x -= y;
  27.  
  28.         System.out.println("Swapped! x is now "+x);
  29.         System.out.println("and y is now "+y);
  30.     }//end main
  31.  
  32. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement