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*/";
}
Comments
Post a Comment