We would need to switch two int values without using the third variable in Java.
For example, we have
int a = 3; int b = 4;
We need to find a way to switch the value between a
and b
without using the third value.
We can do it easily as follow:
int a = 3; int b = 4; int c = a; //using the third variable a = b; b = c;
Find a way without using the third variable.
class Main { public static void main(String args[]) { int a = 3; int b = 4; System.out.println("a="+a); System.out.println("b="+b); a = a + b; b = a - b; a = a - b; System.out.println("a="+a); System.out.println("b="+b); } }