How to Swap Two Numbers without Temp or Third variable in Java - Interview Question

How to swap two numbers without using temp or third variable is common interview question not just on Java interviews but also on C and C++ interviews. It is also a good programming questions for freshers. This question was asked to me long back and didn't had any idea about how to approach this question without using temp or third variable, may be lack of knowledge on bitwise operators in Java or may be it didn't click at that time. Given some time and trial error, I eventually come out with a solution with just arithmetic operator but interviewer was keep asking about other approaches of swapping two variables without using temp or third variable. Personally, I liked this question and included in list of my programming interview questionbecause of its simplicity and some logical work, it force you to do. When learned bit-wise operation in Java I eventually find another way of swapping two variables without third variable, which I am going to share with you guys.
Read more »

1 Response to "How to Swap Two Numbers without Temp or Third variable in Java - Interview Question"

  1. Nice explanation. Thanks for sharing.
    https://www.flowerbrackets.com/java-program-to-swap-two-numbers/

    BalasHapus