Write a program of c++ swapping two variables without using the third variable.
In 3 ways you can do it, let you have x , y as your 2 variable, first approach:- x=x+y; y=x-y; x=x+y; second approach:- x=x*y; y=x/y; x=x/y; third approach:- x=x^y; y=x^y; x=x^y;
Try this. It works in C++ too. https://code.sololearn.com/cx2Zl437LAcu/?ref=app
If we have two variables x and y, then we can do this: x = x + y y = x - y x = x - y I'm not just going to write the program, by the way. This should be enough.
swap(x,y) in algorithm header works with move semantics (C++11) so it changes addresses of x and y.