Steps to Swap Two Numbers in Java
Below are the simple steps we follow:
- Assign x to a temp variable: temp = x
- Assign y to x: x = y
- 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.
Contact Us