+ 1
How to swap two numbers without using a third variable?
it can be done using (+,-) and(*,/).
6 odpowiedzi
+ 8
a, b = b, a
:D
+ 6
You can also use the bitwise xor operator:
a ^= b;
b ^= a;
a ^= b;
// a and b are now swapped
+ 4
/*swapping two numbers without temp variable.
Can also replace + with * and - with / */
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
int a,b;
a=10;
b=20;
cout<<"a and b before swapping "<<a<<" "<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"a and b after swapping "<<a<<" "<<b;
return 0;
}
+ 1
https://code.sololearn.com/cASd5yJTSj7P/?ref=app
- 1
Can try this one out
Let a and be the two integers to be swapped
a = a * b;
b = a / b;
a = a / b: