C++ Program to swap two numbers in a single line


In this program, You will learn how to swap two numbers in a single line in C++.


x ^= y ^= x ^= y

Example: How to swap two numbers in a single line in C++.

#include<iostream>
using namespace std;

int main() {

    int x, y;

    cout << "Enter two numbers:";
    cin >> x >> y;

    x ^= y ^= x ^= y;

    cout << "After Swap x is:" << x;
    cout << "\nAfter Swap y is:" << y;

    return 0;
}

Output:

Enter two numbers:12 13
After Swap x is:13
After Swap y is:12