Definition:-In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory. For example, in a program, two variables may be defined and using a memory place for three variables, and swapping one by one and finally reversing it.
Program:-
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, temp;
cout<<"enter the first number to swap :- ";
cin>>a;
cout<<endl;
cout<<"enter the second number to swap :- ";
cin>>b;
cout<<endl;
cout<<"\n";
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
getch();
}
Output:-
Program:-
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, temp;
cout<<"enter the first number to swap :- ";
cin>>a;
cout<<endl;
cout<<"enter the second number to swap :- ";
cin>>b;
cout<<endl;
cout<<"\n";
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
getch();
}
Output:-
0 Comments