Answer to Question #191943 in C++ for Rahel Aschalew

Question #191943

Swap the content of two variables without using a third variable.


1
Expert's answer
2021-05-14T23:09:40-0400

It can be achieved by two methods one is by Arithmetic method and the other is by Using Bitwise XOR.


I am doing it by using Arithmetic operators-


#include <iostream>
using namespace std; 
  
int main() 
{ 
  int x = 10, y = 50; 


  x = x + y; // x = 60
  y = x - y; // y = 10
  x = x - y; // x = 50
  cout << "After Swapping: x = " << x << ", y = " << y; 
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment