C++ Program to swap two numbers without using third variable. | तीसरे संस्करण का उपयोग किए बिना दो नंबर स्वैप करने के लिए C ++ प्रोग्राम।






















C++ Program to swap two numbers without using third variable.

Let see the program swap two numbers without using third variable.


Example: Declare two variables they are integer or float data types x, y. x=10 y=20 Swap two numbers. X and y values add and store in x x=x+y X and y values subtract and store in y y=x-y; X and y values subtract and store in y x=x-y; After swapping of two numbers x=20 y=10




#include <iostream> using namespace std; int main() { int x,y,z; cout<<"Enter x value :"; cin>>x; cout<<"Enter y value :"; cin>>y; cout<<" Before swapping x value :"<<x<<endl; cout<<" Before swapping y value :"<<y<<endl; x=x+y; y=x-y; x=x-y; cout<<" After swapping x value :"<<x<<endl; cout<<" After swapping y value :"<<y<<endl; return 0; }

Output:

Enter x value :10 Enter y value :20 Before swapping x value :10 Before swapping y value :20 After swapping x value :20 After swapping y value:10