C++ Program to swap two numbers without using third variable
In this program, You will learn how to swap two numbers without using third variable in C++.
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
Example: How to swap two numbers without using third variable in C++.
#include<iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers:";
cin >> num1 >> num2;
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
cout << "After swapping :";
cout << "\nNum1 is :" << num1;
cout << "\nNum2 is :" << num2;
return 0;
}
Output:
Enter two numbers:10 20
After swapping :
Num1 is :20
Num2 is :10