C++ for Beginners: C++ Program to Swap Two Numbers

Hits: 7

(C++ programming Example for Beginners)

C++ Program to Swap Two Numbers

This example contains two different techniques to swap numbers in C programming. The first program uses temporary variable to swap numbers, whereas the second program doesn’t use temporary variables.

Example 1: Swap Numbers (Using Temporary Variable)


#include <iostream>
using namespace std;

int main(){
    int a = 5, b = 10, temp;

    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    temp = a;
    a = b;
    b = temp;

    cout << "nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    return 0;
}

Output

Before swapping.
a = 5, b = 10

After swapping.
a = 10, b = 5

To perform swapping in above example, three variables are used.

The contents of the first variable is copied into the temp variable. Then, the contents of second variable is copied to the first variable.

Finally, the contents of the temp variable is copied back to the second variable which completes the swapping process.

You can also perform swapping using only two variables as below.


Example 2: Swap Numbers Without Using Temporary Variables


#include <iostream>
using namespace std;

int main(){
    
    int a = 5, b = 10;

    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    a = a + b;
    b = a - b;
    a = a - b;

    cout << "nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    return 0;
}

The output of this program is the same as the first program above.

Let us see how this program works:

  1. Initially, a = 5 and b = 10.
  2. Then, we add a and b and store it in a with the code a = a + b. This means a = 5 + 10. So, a = 15 now.
  3. Then we use the code b = a - b. This means b = 15 - 10. So, b = 5 now.
  4. Again, we use the code a = a - b. This means a = 15 - 5. So finally, a = 10.

Hence, the numbers have been swapped.

Note: We can use multiplication and division instead of addition and subtraction. This also gives the same output.

int a = 5, b = 10;

// using multiplication and division for swapping
a = a * b;
b = a / b;
a = a / b;

 

C++ for Beginners: C++ Program to Swap Two Numbers

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.