Swap two values

Swap two Values with third Variable

This is a simple program written in the C++ programming language that swaps the values of two variables.

Code Example

#include <;iostream>
using namespace std;

int main () {
    int x, y, temp;

    cout << "Enter a value for x: ";
    cin >> x;
    cout << "Enter a value for y: ";
    cin >> y;

    cout << "Before swapping:\nX: " << x << "\nY: " << y << endl;  
    
    // swapping numbers
    temp = x;
    x = y;
    y = temp;

    cout << "After swapping:\nX: " << x << "\nY: " << y;    
    
    return 0;
}

Output

Enter a value for x: 22
Enter a value for y: 44
Before swapping:
X: 22
Y: 44
After swapping:
X: 44
Y: 22

Code Explanation

The first section declares three integer variables: x, y, and temp.

The next section uses the cout statement to print the prompt “Enter a value for x: ” and the cin statement to read the value of x from the user. The same process is repeated for the y.

The line cout << "Before swapping:\nX: " << x << "\nY: " << y << endl; displays the original values of x and y to the user.

The next section swaps the values of x and y using a temporary variable temp. The line temp = x; stores the value of x in temp, the line x = y; assigns the value of y to x, and the line y = temp; assigns the value of temp (the original value of x) to y.

The line cout << "After swapping:\nX: " << x << "\nY: " << y; displays the swapped values of x and y to the user.

Finally, the return 0; statement indicates that the program has executed successfully and returns a zero status to the operating system.

Swap two Values without third Variable

This is a simple program written in the C++ programming language that swaps the values of two variables without using a temporary variable.

Code Example

#include <iostream>
using namespace std;

int main () {
    int x, y;

    cout << "Enter a value for x: ";
    cin >> x;
    cout << "Enter a value for y: ";
    cin >> y;

    cout << "Before swapping:\nX: " << x << "\nY: " << y << endl;  
    
    // swapping numbers
    x = x + y;   
    y = x - y;   
    x = x - y; 

    cout << "After swapping:\nX: " << x << "\nY: " << y;    
    
    return 0;
}

Output

Enter a value for x: 22
Enter a value for y: 44
Before swapping:
X: 22
Y: 44
After swapping:
X: 44
Y: 22

Code Explanation

The first section declares two integer variables: x and y.

The next section uses the cout statement to print the prompt “Enter a value for x: ” and the cin statement to read the value of x from the user. The same process is repeated for the y.

The line cout << "Before swapping:\nX: " << x << "\nY: " << y << endl; displays the original values of x and y to the user.

The next section swaps the values of x and y without using a temporary variable.

The line x = x + y; adds the values of x and y and stores the result in x.

The line y = x - y; subtracts the original value of y from the new value of x and stores the result in y.

The line x = x - y; subtracts the new value of y from the new value of x and stores the result in x.

The line cout << "After swapping:\nX: " << x << "\nY: " << y; displays the swapped values of x and y to the user.

Finally, the return 0; statement indicates that the program has executed successfully and returns a zero status to the operating system.