write a program to swap two numbers without using third variable

Two numbers are taken from user and then swapped without using a third variable.


Select To use this code as it is.. select and copy paste this code into code.cpp file :)



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a,b;
  7. cout<<"Enter a: ";
  8. cin>>a;
  9. cout<<"Enter b: ";
  10. cin>>b;
  11. //Coding by: Snehil Khanor
  12. //http://WapCPP.blogspot.com
  13. cout<<"Before swapping:"<<endl;
  14. cout<<"a: "<<a<<" b: "<<b<<endl;
  15. a=a+b;
  16. b=a-b;
  17. a=a-b;
  18. cout<<"After swapping:"<<endl;
  19. cout<<"a: "<<a<<" b: "<<b;
  20. getch();
  21. }

Check out this stream