Ads 468x60px

Saturday 10 December 2011

A C++ program that interchange or swap the values of two integers without using third variable

In this post I am going to write a C++ program that swaps two number without making use of any third variable. The two integers are A and B and the function used is swap(int, int). The program is:
#include 
#include 
// Function to swap A and B without using third variable
void SWAP(int A, int B)
{
 A = A + B;
 B = A - B;
 A = A - B;
 cout << "\n\tAfter swapping a is - > " << A;
 cout << "\n\tAfter swapping b is - > " << B;
 getch();
}
void main()
{
 clrscr();
 int x, y;
 cout << "Enter first number : ";
 cin >> x;
 cout << "Enter second number : ";
 cin >> y;
 SWAP(x, y);
}

Source: www.LearnCandCpp.com

If you have any problem in understanding the concept of the program, you can ask your query by posting it as a response below.

1 comment:

nikhila said...

is the same logic used when we write the program using datatype float and char

Post a Comment