Swap Two Numbers Without Temp Variable

Beginner

Introduction

Swapping two numbers means that their values should be interchanged, i.e. the initial value of the first number should now be what the second number was, and vice versa. In this lab, we will learn two ways of swapping two numbers without using a third variable:

Create a new C++ file

Open your terminal and navigate to the project directory with the command:

cd ~/project

Create a new C++ file named swap.cpp using the command:

touch swap.cpp

Open swap.cpp in your preferred code editor.

Write the code for swapping two numbers using + and - operators

In this step, we will write the code for swapping two numbers using + and - operators. The code should take two numbers from the user and swap them without using a third variable.

// swap.cpp
#include <iostream>
using namespace std;

void swap_numbers(int &x, int &y) {
    x = x + y;
    y = x - y;
    x = x - y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap_numbers(a, b);

    cout << "After swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

In this code, we have created a function swap_numbers which takes two integer arguments and performs the swapping operation. We take two numbers a and b from the user and call the function swap_numbers which swaps their values. Finally, we print the swapped values of a and b.

To run the program, use the command:

g++ swap.cpp -o swap && ./swap

Write the code for swapping two numbers using * and / operators

In this step, we will write the code for swapping two numbers using * and / operators. The code should take two numbers from the user and swap them without using a third variable.

// swap.cpp
#include <iostream>
using namespace std;

void swap_numbers(int &x, int &y) {
    x = x * y;
    y = x / y;
    x = x / y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap_numbers(a, b);

    cout << "After swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
 }

In this code too, we have created a function swap_numbers which takes two integer arguments and performs the swapping operation. We take two numbers a and b from the user and call the function swap_numbers which swaps their values. Finally, we print the swapped values of a and b. In this method of swapping, we use multiplication and division operations instead of addition and subtraction.

To run the program, use the command:

g++ swap.cpp -o swap && ./swap

Final program

Here is the full code for swap.cpp program that swaps two numbers both by addition-subtraction and multiplication-division methods.

// swap.cpp
#include <iostream>
using namespace std;

void add_subtract_swap(int &x, int &y) {
    x = x + y;
    y = x - y;
    x = x - y;
}

void multiply_divide_swap(int &x, int &y) {
    x = x * y;
    y = x / y;
    x = x / y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    add_subtract_swap(a, b);

    cout << "After swapping using + and - operators: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    multiply_divide_swap(a, b);

    cout << "After swapping using * and / operators: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

Compile and run

To compile and run the program, use the command:

g++ swap.cpp -o swap && ./swap

The program will ask the user for two numbers, which it will swap both the addition-subtraction and multiplication-division ways. The output will show the initial values of a and b, the swapped values using addition-subtraction, and the swapped values using multiplication-division.

Summary

Swapping is an important operation in programming, and it is often required in many algorithms. In this lab, we learned two ways of swapping two numbers without using a third variable: using addition-subtraction, and using multiplication-division. We also learned how to create functions to perform the swapping operation, making our code more modular and easier to read.

Other Tutorials you may like