How to swap two numbers without using a third variable

There are many ways to swap two numbers without using a third variable.

1. Using arithmetic operators

Swapping two variables without using a third variable.
Swapping two variables without using a third variable.

The arithmetic operators for addition and subtraction can be used to perform the swap without using a third variable.

Similarly, multiplication and division can be used to perform the swap without using the third variable.

2. Using Bitwise XOR

The result of the bitwise XOR operator is 11 if the corresponding bits of two operands are opposite. It is denoted by ^.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved