Contact

Swap Two Numbers in C++

In this article, we will explore various methods to swap two numbers in the C++ programming language. We'll cover swapping using a temporary variable, without using a temporary variable, and demonstrate both call by value and call by reference approaches.

Swap Numbers Using Temporary Variable in C++

Let's start by examining a C++ program that swaps two numbers using a temporary variable.

#include <iostream>
using namespace std;

int main() {
    double first, second, temp;

    // Prompting the user to enter the first number
    cout << "Enter first number: ";
    cin >> first;

    // Prompting the user to enter the second number
    cout << "Enter second number: ";
    cin >> second;

    // Swapping using a temporary variable
    temp = first;
    first = second;
    second = temp;

    // Displaying the swapped numbers with two decimal places
    cout << "\nAfter swapping, first number = " << fixed << setprecision(2) << first << endl;
    cout << "After swapping, second number = " << fixed << setprecision(2) << second << endl;

    return 0;
}

This C++ program prompts the user to enter two numbers, swaps them using a temporary variable, and then displays the swapped numbers formatted to two decimal places using cout with fixed and setprecision manipulators.

Let's break down what happens in the above C++ code:

Therefore, the output of this program will be:

Enter first number: 4.5
Enter second number: 7.8

After swapping, first number = 7.80
After swapping, second number = 4.50

Swap Numbers Without Using Temporary Variable in C++

Now, let's explore another method to swap numbers without using any additional temporary variable.

C++ Code
#include <iostream>
using namespace std;

int main() {
    double a, b;

    // Prompting the user to enter value for 'a'
    cout << "Enter a: ";
    cin >> a;

    // Prompting the user to enter value for 'b'
    cout << "Enter b: ";
    cin >> b;

    // Swapping without using a temporary variable
    a = a - b;   // 'a' now holds the difference between initial 'a' and 'b'
    b = a + b;   // 'b' now holds the original value of 'a'
    a = b - a;   // 'a' now holds the original value of 'b'

    // Displaying the swapped values with two decimal places
    cout << "\nAfter swapping, a = " << fixed << setprecision(2) << a << endl;
    cout << "After swapping, b = " << fixed << setprecision(2) << b << endl;

    return 0;
}
Output
Enter a: 10
Enter b: 20

After swapping, a = 20.00
After swapping, b = 10.00

Swap Numbers Using Call by Value in C++

In the call by value method, the function receives copies of the values of the variables. Swapping is done within the function using these copies, which does not affect the original variables in the calling function.

C++ Code
#include <iostream>
using namespace std;

// Function prototype for swapping using call by value
void swapByValue(double num1, double num2);

int main() {
    double first, second;

    // Prompting the user to enter the first number
    cout << "Enter first number: ";
    cin >> first;

    // Prompting the user to enter the second number
    cout << "Enter second number: ";
    cin >> second;

    // Displaying the original values
    cout << "\nOriginal values: first = " << fixed << setprecision(2) << first << ", second = " << second << endl;

    // Calling the swap function with call by value
    swapByValue(first, second);

    // Displaying the swapped values (original values remain unchanged)
    cout << "After swapping by value: first = " << fixed << setprecision(2) << first << ", second = " << second << endl;

    return 0;
}

// Function definition for swapping using call by value
void swapByValue(double num1, double num2) {
    double temp;

    temp = num1;
    num1 = num2;
    num2 = temp;

    cout << "After swapping by value (inside the function): first = " << fixed << setprecision(2) << num1 << ", second = " << num2 << endl;
}
Output
Enter first number: 10
Enter second number: 50

Original values: first = 10.00, second = 50
After swapping by value (inside the function): first = 50.00, second = 10.00
After swapping by value: first = 10.00, second = 50

The swapByValue function receives copies of num1 and num2. Swapping is performed using a temporary variable within the function. Changes inside swapByValue do not affect the original values of first and second in main.

Swap Numbers Using Call by Reference in C++

In the call by reference method, the function receives references (or pointers) to the variables. Swapping is done using these references, allowing modification of the original variables in the calling function.

C++ Code
#include <iostream>
using namespace std;

// Function prototype for swapping using call by reference
void swapByReference(double &num1, double &num2);

int main() {
    double first, second;

    // Prompting the user to enter the first number
    cout << "Enter first number: ";
    cin >> first;

    // Prompting the user to enter the second number
    cout << "Enter second number: ";
    cin >> second;

    // Displaying the original values
    cout << "\nOriginal values: first = " << fixed << setprecision(2) << first << ", second = " << second << endl;

    // Calling the swap function with call by reference
    swapByReference(first, second);

    // Displaying the swapped values (original values are modified)
    cout << "After swapping by reference: first = " << fixed << setprecision(2) << first << ", second = " << second << endl;

    return 0;
}

// Function definition for swapping using call by reference
void swapByReference(double &num1, double &num2) {
    double temp;

    temp = num1;
    num1 = num2;
    num2 = temp;
}
Output
Enter first number: 10
Enter second number: 50

Original values: first = 10.00, second = 50
After swapping by reference: first = 50.00, second = 10.00

Inside swapByReference, the values of first and second are swapped using call by reference, which directly modifies the original variables first and second.




Sharing is stylish! Gift your friends this awesome read!