Swapping two integer value in C++

We are going to write a program that swap the two integers. So let us start.

Final Output




First we're going to add the header file or the library of iostream header file (Input Output Stream).
for standardization we have to include or use the namespace keyword.
the purpose of int main() is to get the return value of our process. 
declare the variable swap1 & swap2 as Integer datatype.
prompt the user to input a value for A & B with extraction operator <<.
with the insertion operator >> to assign a value from the variable and get the input for process.


#include <iostream>
using namespace std;

int main()
{
       int swap1;
       int swap2;

cout<<"Input value of integer A : ";
cin>>swap1;
cout<<"Input value of integer B : ";
cin>>swap2;

cout<<"\nBefore swap the value of A : "<<swap1<<" & B : "<<swap2;

swap2 = swap1 + swap2;
swap1 = swap2 - swap1;
swap2 = swap2 - swap1;

cout<<"\nAfter swap the Value of A : "<<swap1<<" & B : "<<swap2;
return 0;
}

Swapping two integer value in C++ Swapping two integer value in C++ Reviewed by code-dev on 11:45 AM Rating: 5

No comments:

Powered by Blogger.