Pages - Menu

Pages

Wednesday, December 16, 2015

Java program to swap the values of two variables without temporary variable


Swapping the values of two variables without temp variable:

import java.util.*;
public class Swapping
{
  public static void main()
  {
      int x,y;
      Scanner sc=new Scanner(System.in);
      System.out.println("Please enter the numbers you want to swap values of");
      x=sc.nextInt();
      y=sc.nextInt();
      x = x + y;
      y = x - y;
      x = x - y;
      System.out.println("Swapped values are: " +x+", "+y);
     
  }
}
Thank you and make sure to like and comment.

No comments:

Post a Comment