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++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int x, y;

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

    x ^= y ^= x ^= y;

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

    return 0;
}

Output:

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