Swap Two Numbers Without using Third Variable in C++ language
ADVERTISEMENTS
Swap two numbers without using third variable in c++ language. In this article, you will learn how to swap two numbers without using third variable in c++ language.
Source Code
// Swap Two Numbers Without using Third Variable in C++ language
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double p, q;
cout << "----Enter the two integer or real numbers----\n";
cin >> p >> q;
p = p - q;
q = p + q;
p = q - p;
std::cout << std::fixed << std::setprecision(2) << "After swapping, p = " << p << "\n";
std::cout << std::fixed << std::setprecision(2) << "After swapping, q = " << q << "\n";
return 0;
}
Output
----Enter the two integer or real numbers----
7
10
After swapping, p = 10.00
After swapping, q = 7.00
7
10
After swapping, p = 10.00
After swapping, q = 7.00