In this tutorial, we will see “How to Swap two numbers using a third variable in Java?”
swapping of two numbers using third variable in java
Java – Swap two numbers using a third variable? Share on X
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Using Java, Swap two numbers using a third variable * @author Deepak Verma * */ public class Swap_Two_Number_Using_Third_Variable_Java { public static void main(String[] args) { int number1 = 10; int number2 = 20; System.out.println("Before swapping:\n number1 = " + number1 + " and number2 = " + number2); int temp = number1; number1 = number2; number2 = temp; System.out.println("After swapping:\n number1 = " + number1 + " and number2 = " + number2); } } |
Output:
1 2 3 4 | Before swapping: number1 = 10 and number2 = 20 After swapping: number1 = 20 and number2 = 10 |
swap two numbers using third variable in java
Java – Swap two numbers using a third variable? Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: