Swap Two Numbers using Function in C++ Last Updated : 02 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 the address or reference of the two variables that we want to swap to the function. If we pass them as value, we won't be able to change their value as in pass by value method, no changes are reflected in the original variables. After that, we can use the temporary variable to swap the value of the given number of variables. C++ Program for Swapping Two Numbers Using Function C++ // C++ program to illustrate how to swap two variables using // a function in C++ #include <iostream> using namespace std; // function to swap two variables void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // driver code int main() { int a = 10; int b = 22; cout << "Before Swapping: " << endl; cout << " a: " << a << " b: " << b << endl; // calling swap swap(&a, &b); cout << "After Swapping: " << endl; cout << " a: " << a << " b: " << b << endl; return 0; } OutputBefore Swapping: a: 10 b: 22 After Swapping: a: 22 b: 10 We can also make use of C++ templates to create a function that can swap the values of any kind. C++ Program for Swapping Using Function Template C++ // C++ program to illustrate how to swap two numbers using // function template #include <iostream> #include <string> using namespace std; // function template to swap two values template <typename T> void swap_func(T& a, T& b) { T temp = a; a = b; b = temp; } // driver code int main() { string s1 = "Geeks", s2 = "for"; int num1 = 10, num2 = 22; cout << "Before Swapping:" << endl; cout << s1 << " " << s2 << endl; cout << num1 << " " << num2 << endl; swap_func(s1, s2); swap_func(num1, num2); cout << "\nAfter Swapping:" << endl; cout << s1 << " " << s2 << endl; cout << num1 << " " << num2 << endl; return 0; } OutputBefore Swapping: Geeks for 10 22 After Swapping: for Geeks 22 10 Comment More infoAdvertise with us Next Article Swap Two Numbers using Function in C++ A abhishekcpp Follow Improve Article Tags : C++ Programs C++ cpp-template Practice Tags : CPP Similar Reads 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 valarray swap() function in c++ The swap() function is defined in valarray header file. This function is used to swap the content of one valarray with another valarray. Syntax: void swap( valarray& valarray2 ); Parameter: This method accepts a parameter valarray2 which represents the another valarray with which we have to swap 2 min read Swap Two Numbers Without Third Variable in C++ 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 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 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 swap Keys with Values of a Map in C++? Given a map, the task is to swap the Keys of this map with its values, in C++. Examples: Input: map = {'e', 1 }, {'o', 1 }, {'r', 3 }, Output: {1, 'e' }, {1, 'o' }, {3, 'r' }, Method 1: By using vector pair, traverse through the given map push_back the swapped values in the vector pair sort the vect 4 min read Reverse a Number in C++ In this article, we will learn to write a C++ program to reverse a number. Reversing the digits of a number means changing the order of the digits of the number so that the last digit becomes the first digit, the second last digit becomes the second digit, and so on. The number upon reversing read t 2 min read C++ Program to Swap characters in a String Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your ta 6 min read Like