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

Question #191928

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


1
Expert's answer
2021-05-13T19:03:08-0400
#include <iostream>


using namespace std;


int main()
{
      //get the two numbers from the user
      int n1,n2;
      cout<<"Enter the first number: ";
      cin>>n1;
      cout<<"\nEnter the second number: ";
      cin>>n2;
      
     //print the numbers before swapping
      cout<<"Before swapping:\n"<<"n1= "<<n1<<"\tn2= "<<n2<<endl;
      //swap the numbers
      n1 = n1 + n2;   
      n2 = n1 - n2;    
      n1 = n1 - n2; 
      
      //print the numbers after swapping
      cout<<"After swapping:\n"<<"n1= "<<n1<<"\tn2= "<<n2<<endl;


    return 0;
}

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