Swap Two Numbers Without Third Variable in C++ Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two numbers without using a temporary variable by using simple addition and subtraction arithmetic trick as shown. C++ Program to Swap Two Numbers Without Using Temporary VariableThe below program shows how we can swap two numbers without using a temporary variable. C++ // C++ program to swap two numbers without using temporary // variable #include <iostream> using namespace std; int main() { // numbers to be swapped int a = 5, b = 10; int x = 50, y = 100; cout << "Before swapping: x = " << x << ", y = " << y << endl; // performing swap using addition substraction method x = x + y; y = x - y; x = x - y; // print values after swapping cout << "After swapping: x = " << x << ", y = " << y << endl << endl; return 0; } OutputBefore swapping: x = 50, y = 100 After swapping: x = 100, y = 50 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Swap Two Numbers Without Third Variable in C++ T the_star_emperor Follow Improve Article Tags : C++ Programs C++ Swap-Program CPP Examples Practice Tags : CPP Similar Reads How to Swap Two Strings Without Using Third String in C++? In C++, a string is a sequence of characters. Swapping two strings typically involves using a third string as temporary storage. However, itâs possible to swap two strings without using a third string. In this article, we will learn how to do this in C++. Example Input: str1 = "Hello" str2 = "world" 2 min read Swap Two Numbers using Function in C++ Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass t 3 min read How to Swap Two Numbers Using Pointers in C++? Swapping the values of two numbers is a very common operation in programming, which is often used to understand the basics of variables, pointers, and function calls. In this article, we will learn how to swap two numbers using pointers in C++. Example Input:int x=10;int y=20;Output:int x=20;int y=1 3 min read C++ Program to Swap Two Numbers Swapping numbers is the process of interchanging their values. In this article, we will learn algorithms and code to swap two numbers in the C++ programming language.1. Swap Numbers Using a Temporary VariableWe can swap the values of the given two numbers by using another variable to temporarily sto 3 min read Swap Two Variables Using XOR In C++, variables are containers that store data. Sometimes, we may need to swap the values of two variables. The traditional method involves using a third variable as a temporary storage to facilitate the swap. In this article, we will learn how to swap two variables using the XOR technique. Exampl 2 min read Like