Please disable your adblock and script blockers to view this page
 

How to swap two numbers in C++ Language ?

Introduction: In my previous article I explained What is C++ Language ? Calculate sum of two numbers in C++ Language  and Swapping two numbers without using temporary variable and Another way to Swap two numbers without using temporary variable .
Now in this article I have explained How to swap/interchange two numbers/values in C++ Language. Swapping of two numbers in C++ is not a difficult task.

Implementation: Let's create a vary basic  program to swap two number.

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a,b,temp;
 cout<<"Enter first number= ";
 cin>>a;
 cout<<"Enter second number= ";
 cin>>b;
 cout<<"\nValue of first number before swapping= "<<a;
 cout<<"\nValue of second number before swapping= "<<b;
 temp=a;
 a=b;
 b=temp;
 cout<<"\nValue of first number after swapping= "<<a;
 cout<<"\nValue of second number after swapping= "<<b;
 getch();
 }

Run the program using ctrl+F9

Output:
Enter first number= 20
Enter second number= 50

Value of first number before swapping= 20
Value of second number before swapping= 50
Value of first number after swapping= 50
Value of second number after swapping= 20

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon