Program to swap two numbers using pass by reference
OUTPUT
#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y); // function declaration
void main()
{
int a,b;
clrscr();
cout<<"Enter two numbers a & b"<<endl;
cin>>a>>b;
cout<<"\nBefore swapping:\n";
cout<<"a="<<a;
cout<<"\nb="<<b;
swap(a,b); //function call
cout<<"\nAfter swapping:\n";
cout<<"a="<<a;
cout<<"\nb="<<b;
getch();
}
void swap(int &x,int &y) // arguments by reference
{
int t;
t=x;
x=y;
y=t;
}
#include<conio.h>
void swap(int &x,int &y); // function declaration
void main()
{
int a,b;
clrscr();
cout<<"Enter two numbers a & b"<<endl;
cin>>a>>b;
cout<<"\nBefore swapping:\n";
cout<<"a="<<a;
cout<<"\nb="<<b;
swap(a,b); //function call
cout<<"\nAfter swapping:\n";
cout<<"a="<<a;
cout<<"\nb="<<b;
getch();
}
void swap(int &x,int &y) // arguments by reference
{
int t;
t=x;
x=y;
y=t;
}
OUTPUT
No comments:
Post a Comment