Search This Blog

Monday, February 11, 2013

swap using a third variable (C++)



#include<iostream.h>
#include<conio.h>
int main()
{
int a,b,c;
clrscr();
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
cout<<"Before swapping:"<<endl;
cout<<"a: "<<a<<" b: "<<b<<endl;
c=a;
a=b;
b=c;
cout<<"After swapping:"<<endl;
cout<<"a: "<<a<<" b: "<<b;
getch();
return 0;
}
Output:




Enter a: 1
Enter b: 2
Before swapping:
a: 0 b: 1
After swapping:
a: 1 b: 0

No comments:

Post a Comment