C++ Exercises
C++ Program to swap two numbers using the third variable.
Let see the program swap two numbers using the third variable.
Example: Declare three variables they are integer or float data types x, y, z. x=10 y=20 Swap two numbers. z=x; X value store in z x=y; y value store in x y=z z value store in 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; z=x; x=y; y=z; 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