C++ program to swap values of two variables using pass by reference method

C++ program to swap values of two variables using pass by reference method

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b;
void swap(int &,int &);
cout<<“Enter two values:”;
cin>>a>>b;

cout<<“nBefor swapping:na=”<<a<<“tb=”<<b;
swap(a,b);
cout<<“nnAfter swapping:na=”<<a<<“tb=”<<b;
getch();
}

void swap(int & x,int & y)
{
int temp;
temp=x;
x=y;
y=temp;
}

6 thoughts on “C++ program to swap values of two variables using pass by reference method”

  1. Great tutorial, love how it’s well explained. Altho error coming on output, so please do update the article. Thanks 🙂

  2. /* —- Here is the correct program —- */
    #include
    #include

    using namespace std;

    int main()
    {
    system(“cls”);

    int a,b;
    void swap(int &,int &);
    cout<>a>>b;

    cout<<"\nBefor swapping:\na="<<a<<"\tb="<<b;
    swap(a,b);
    cout<<"\n\nAfter swapping:\na="<<a<<"\tb="<<b;
    getch();
    return 0;
    }

    void swap(int & x,int & y)
    {
    int temp;
    temp=x;
    x=y;
    y=temp;
    }

  3. #include
    #include

    void main()
    {

    // clrscr();

    int a,b;
    void swap(int &,int &);
    cout<>a>>b;

    cout<<"nBefor swapping:na="<<a<<"tb="<<b;
    swap(a,b);
    cout<<"nnAfter swapping:na="<<a<<"tb="<<b;
    getch();
    }

    void swap(int & x,int & y)
    {
    int temp;
    temp=x;
    x=y;
    y=temp

Leave a Comment

Your email address will not be published. Required fields are marked *