7.c++ program to swap two numbers using 6 methods


SWAPPING NUMBERS USING 6 METHODS:


FIRST METHOD:


// C++ program to swap numbers using temporary variable


INPUT:


#include<iostream>

using namespace std;

int main()

{

    int x,y,z;

    cout<<"Enter The Value of x : ";

    cin>>x;

    cout<<"Enter The Value of y : ";

    cin>>y;

    cout<<"\n\t Before Swapping"<<endl;

    cout<<"x = "<<x<<endl;

    cout<<"y = "<<y<<endl;

    z=x;

    x=y;

    y=z;

    cout<<"\n\t After Swapping"<<endl;

    cout<<"x = "<<x<<endl;

    cout<<"y = "<<y<<endl;

    return 0;

}


SECOND METHOD:



// C++ program to swap numbers without using Temporary variables using addition and subtraction operator


INPUT:

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cout<<"Enter The Value of x : ";
    cin>>x;
    cout<<"Enter The Value of y : ";
    cin>>y;
    cout<<"\n\t Before Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    x = x + y;
    y = x - y;
    x = x - y;
    cout<<"\n\t After Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    return 0;
}


THIRD METHOD:

// C++ program to swap numbers without using temporary variable using multiplication and division operator


INPUT:

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cout<<"Enter The Value of x : ";
    cin>>x;
    cout<<"Enter The Value of y : ";
    cin>>y;
    cout<<"\n\t Before Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    x = x * y;
    y = x / y;
    x = x / y;
    cout<<"\n\t After Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    return 0;
}


FOURTH METHOD:


// C++ program to swap numbers using bitwise addition and subtraction


INPUT:

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cout<<"Enter The Value of x : ";
    cin>>x;
    cout<<"Enter The Value of y : ";
    cin>>y;
    cout<<"\n\t Before Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    x = (x & y) + (x | y);
    y = x + (~y) + 1;
    x = x + (~y) + 1;
    cout<<"\n\t After Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    return 0;
}


FIFTH METHOD:


// C++ program to swap numbers using swap( ) function


INPUT:

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cout<<"Enter The Value of x : ";
    cin>>x;
    cout<<"Enter The Value of y : ";
    cin>>y;
    cout<<"\n\t Before Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    swap(x,y);
    cout<<"\n\t After Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    return 0;
}


SIXTH METHOD:


// C++ program to swap numbers using bitwise XOR


INPUT:

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    cout<<"Enter The Value of x : ";
    cin>>x;
    cout<<"Enter The Value of y : ";
    cin>>y;
    cout<<"\n\t Before Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
    cout<<"\n\t After Swapping"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
    return 0;

}

For all this program same output : 














 

Comments