Steps to Swap Two Numbers in Java

Below are the simple steps we follow: 

  1. Assign x to a temp variable: temp = x 
  2. Assign y to x: x = y 
  3. Assign temp to y: y = temp

Java Program to Swap two Variables

Given two numbers x and y, we need to swap their values. In this article, we will learn the Swapping of two numbers in Java.

Examples of Swapping

Input : x = 10, y = 20;
Output : x = 20, y = 10
Input : x = 213, y = 109
Output : x = 109, y = 213

Recommended: Please try your approach on {IDE} first, before moving on to the solution. 

Similar Reads

Steps to Swap Two Numbers in Java

Below are the simple steps we follow:...

Methods for Swapping of Two Numbers in Java

1. Using Three Variables...

References

...

Contact Us