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

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex