20 March, 2012

C++ Program For Swapping Of Two Integers.

This Post Contains A C++ Program For Swapping Of Two Integers Without Using Third Variable With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Swapping' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result. 


C++ Program For Swapping Of Two Integers.

/* Declaration Of Header Files */
# include <iostream.h>
# include <conio.h>

/* Start Of Main Program */
void main()
{
  
   /* Declaration Of Variables */
   int   i,   j;
   clrscr();

   /* Asking For The Input From User */
   cout << "  Enter Any 2 Numbers   :   ";
   cin >>   i >>   j;
   cout << " \n  Before Swapping   :   ";
   cout <<   i <<   j;

 /* Source Code For Computing Swapping Of Two Integers */
   cout << " \n  After Swapping   :   ";
   i   =   i   +   j ;
   j   =   i   -   j ;
   i   =   i   -   j ;
   cout <<   i <<   j;
  
   getch();
}
/* End Of Main Program */

Output :

Enter Any 2 Numbers :  12   21

Before Swapping :  12   21

After Swapping :  21    12    





No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.