Swap two variables without using a temporary or additional values

This is normal programming practice.

Solution:
public class SwapVariables{
public static void main(String[] args) {
int a = 2,b = 4;
        System.out.println("Before Swap ");
        System.out.println("Value of A: "+a);
        System.out.println("Value of B: "+b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("After Swap ");
        System.out.println("Value of A: "+a);
        System.out.println("Value of B: "+b);
}
}

Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

Dependencies vs Dev Dependencies in Flutter

What's new in android 14?