How to Swap Two Numbers Using Pointers in C++? Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report 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=10;Swap Two Numbers Using Pointers in C++In C++, pointers are variables that store the memory address of another variable. Using pointers the value of variables can be changed directly as we have direct access to the address of the variables. Using this feature of pointers we will swap two numbers. Following is the algorithm we will follow to swap two numbers using pointers in C++: AlgorithmDeclare a function swap that takes two integer pointers x and y as parameters.Use a temporary variable temp to store the value of x.temp=*x;Store the value of y in x by dereferncing the pointers.*x=*yStore the value of temp in y.*y=temp;Call the swap function by passing the address of x and y.If you are not familiar with the concept of dereferencing, you can read the following article --> C++ Dereferencing C++ Program to Swap Two Numbers using PointersThe following program illustrates how we can swap two numbers using pointers in C++: C++ // C++ Program to Swap Two Numbers using Pointers #include <iostream> using namespace std; // Function to swap two Numbers using pointers void swap(int* x, int* y) { // Store the value pointed to by x in temp int temp = *x; // Assign the value pointed by y to the location pointed to by x *x = *y; // Assign the value stored in temp to the location pointed to by y *y = temp; } int main() { // Initialize two numbers x and y int x = 5, y = 10; // Print values before swapping cout << "Before swapping: x = " << x << ", y = " << y << endl; // Call the swap function, passing the addresses of x and y swap(&x, &y); // Print values after swapping cout << "After swapping: x = " << x << ", y = " << y << endl; return 0; } OutputBefore swapping: x = 5, y = 10 After swapping: x = 10, y = 5 Time Complexity: O(1)Auxiliary Space: O(1) Want to learn about other approaches to swap two numbers? Refer to the following articles: Swap two numbers without using temp variableC Program to Swap two numbers Comment More infoAdvertise with us Next Article How to Swap Two Numbers Using Pointers in C++? H heysaiyad Follow Improve Article Tags : C++ Programs C++ CPP-Basics CPP Examples misc-cpp +1 More Practice Tags : CPP Similar Reads 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 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 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 How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read Like