Program to interchange the values of two numbers: (a)w/o 3rd variable (b)with 3rd variable

a.Without 3rd Variable

#include<iostream>
using namespace std;
int main()
{
   int a,b;
   cout<<"Enter the value of a\n";
   cin>>a;
   cout<<"Enter the value of b\n";
   cin>>b;
   cout<<"value of a and b before swap is :\nA = "<<a<<endl<<"B = "<<b<<endl;
   b=a+b;
   a=b-a;
   b=b-a;
   cout<<"value of a and b after swap is :\nA = "<<a<<endl<<"B = "<<b<<endl;
   cout<<"/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
}

b.With 3rd Variable

#include<iostream>
using namespace std;
int main()
{
   int a,b,temp;
   cout<<"Enter the value of a\n";
   cin>>a;
   cout<<"Enter the value of b\n";
   cin>>b;
   cout<<"value of a and b before swap is :\nA = "<<a<<endl<<"B = "<<b<<endl;
   temp=b;
   b=a; 
  a=temp;
   cout<<"value of a and b after swap is :\nA = "<<a<<endl<<"B = "<<b<<endl;
   cout<<"/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
}


*Output:


*Note: Output would be same for both 

Comments

Popular posts from this blog

Example of Synopsis in LaTeX

Program to display the different colors using the switch statement.

Program to illustrate the concept of templates.