Swap 2 Numbers | 5 Ways To Swap Two Numbers In Java
Way -1==========
public class swap2Numbers{ public static void main(String[] args) { int a = 10, b =20; System.out.println("Before Swapping:" + a + "..." + b);
// using third variable approach int temp =a; a=b; b=temp; System.out.println("After swapping:" + a +"..." + b);
}
}
Way -2============
public class swap2Numbers2 {
// without using third variable approach
public static void main(String[] args) { int a = 10, b = 20; System.out.println("Before Swapping:" + a + "..." + b); a = a + b; b = a - b; a = a - b;
System.out.println("After Swapping:" + a + "..." + b);
}
}
Way -3============
public class swap2Numbers3 {
// using multiplication and division operator approach public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("Before Swapping:" + a + "..." + b); a = a * b; b = a / b; a = a / b;
System.out.println("After Swapping:" + a + "..." + b);
}
}
Way -4================
public class swap2Numbers4 {
public static void main(String[] args) {
// using Bit-wise X-OR operator int a = 10 // Binary Representation 1010, b = 20 // Binary Representation 10100; /* * a^b * 01010 * ^10100 * ============= * 11110 * Like this way, * */ System.out.println("Before Swapping:" + a + "..." + b); a = a ^ b; b = a ^ b; a = a ^ b;
System.out.println("After Swapping:" + a + "..." + b);
}
}
Way -5==============
public class swap2Numbers5 {
public static void main(String[] args) { int a = 10, b = 20; // with single statement approach System.out.println("Before Swapping:" + a + "..." + b); b = a + b - (a = b); // This expression execution starts from Right to left System.out.println("After swapping:" + a + "..." + b);
}
}
Swap 2 Numbers | 5 Ways To Swap Two Numbers In Java
Way -1
==========
public class swap2Numbers
{
public static void main(String[] args) {
int a = 10, b =20;
System.out.println("Before Swapping:" + a + "..." + b);
// using third variable approach
int temp =a;
a=b;
b=temp;
System.out.println("After swapping:" + a +"..." + b);
}
}
Way -2
============
public class swap2Numbers2 {
// without using third variable approach
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("Before Swapping:" + a + "..." + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swapping:" + a + "..." + b);
}
}
Way -3
============
public class swap2Numbers3 {
// using multiplication and division operator approach
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("Before Swapping:" + a + "..." + b);
a = a * b;
b = a / b;
a = a / b;
System.out.println("After Swapping:" + a + "..." + b);
}
}
Way -4
================
public class swap2Numbers4 {
public static void main(String[] args) {
// using Bit-wise X-OR operator
int a = 10 // Binary Representation 1010, b = 20 // Binary Representation 10100;
/*
* a^b
* 01010
* ^10100
* =============
* 11110
* Like this way,
*
*/
System.out.println("Before Swapping:" + a + "..." + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("After Swapping:" + a + "..." + b);
}
}
Way -5
==============
public class swap2Numbers5 {
public static void main(String[] args) {
int a = 10, b = 20;
// with single statement approach
System.out.println("Before Swapping:" + a + "..." + b);
b = a + b - (a = b);
// This expression execution starts from Right to left
System.out.println("After swapping:" + a + "..." + b);
}
}
Comments
Post a Comment