Monday, 26 September 2016

Swapping Without Third Variable

C++ PROGRAMS


The previous program (Swapping) used a third variable t which helped in swapping. However swapping can also be done without a third variable. 

In this we use simple logic of addition and subtraction.

Code:

#include<iostream.h>
#include<conio.h>

void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;

cout<<a<<" "<<b;

}

void main()
{
int a,b;
cin>>a>>b;
swap(a,b);

getch();

}


Output:

First Run:

Second Run:







For any queries or suggestions please comment !

No comments:

Post a Comment