-->





C++ program to swap the values of two integers

Logic of the code:
  •     Take three variables var1,var2 and swap.
  •     Store value of var1 in swap.
  •     Store value of var2 in var1.
  •     Copy value of swap into var2 which is value of var1.

C++ program code to swap the values of two integer:

#include<iostream>

using namespace std;

int main()

{

int var1, var2, swap;

 cout<<"Enter value for first integer:  ";

 cin>>var1;

 cout<<"Enter value for second integer:  ";

 cin>>var2;

 cout<<" Values Before swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

              swap=var1;

              var1=var2;

              var2=swap;

 cout<<" Values After swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

 return 0;

}Sample Output:

Image of Code:

 

5 comments: