- 1
Write a program of c++ swapping two variables without using the third variable.
4 odpowiedzi
+ 5
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;
+ 4
Try this. It works in C++ too.
https://code.sololearn.com/cx2Zl437LAcu/?ref=app
+ 3
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.
+ 1
swap(x,y) in algorithm header works with move semantics (C++11) so it changes addresses of x and y.