3.9

#include<iostream>
using namespace std;
void swap1(int x,int y)
{
    x=x+y;
    y=x-y;
    x=x-y;
}
void swap2(int *x,int *y)
{
    *x=*x+*y;
    *y=*x-*y;
    *x=*x-*y;
}

void swap3(int &x,int &y)
{
    x=x+y;
    y=x-y;
    x=x-y;
}
int main()
{
   
    cout<<"enter two numbers:";
    int x,y;
    cin>>x>>y;
    cout<<"-------menu-------"<<endl;
    cout<<"1.call by value\n";
    cout<<"2.call by address\n";
    cout<<"3.call by reference\n";
    cout<<"enter the choice:";
    int ch;
    cin>>ch;
    if(ch==1)
    {
        swap1(x,y);
        cout<<"u have selected call by value";
        cout<<" so there will be no change to actual parameter"<<endl;
        cout<<"after swap"<<endl;
        cout<<"1.number ="<<x<<endl;
        cout<<"2.number ="<<y<<endl;
       
    }
    else if(ch==2)
    {
        swap2(&x,&y);
        cout<<"u have selected call by address";
        cout<<" so there will be change to actual parameter"<<endl;
        cout<<"after swap"<<endl;
        cout<<"1.number ="<<x<<endl;
        cout<<"2.number ="<<y<<endl;
    }
    else if(ch==3)
    {
        swap3(x,y);
        cout<<"u have selected call by reference";
        cout<<" so there will be change to actual parameter"<<endl;
        cout<<"after swap"<<endl;
        cout<<"1.number ="<<x<<endl;
        cout<<"2.number ="<<y<<endl;
    }
    else
        cout<<"invalid choice:"<<endl;
    return 0;
}

Total Pageviews