Aug 28, 2015

Program in C++ to swap two values using pointers and reference variable

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void swappoint(int *,int *);
void swapref(int &,int &);
void main()
{
int a,b;
clrscr();
cout<<"Enter the two num "<<endl;
cin>>a>>b;
cout<<"\n values of A & B before calling function"<<endl;
cout<<"A= "<<a<<"\t"<<"B= "<<b;
swappoint(&a,&b);
cout<<"\n The values of A & B after calling swapoint()"<<endl;
cout<<"A= "<<a<<"\t"<<"B= "<<b;
swapref(a,b);
cout<<"\n The values of A & B after calling swapref()"<<endl;
cout<<"A= "<<a<<"\t"<<"B= "<<b;
getch();
}
void swappoint(int *l,int *m)
{
int k;
k=*l;
*l=*m;
*m=k;
}
void swapref(int &l,int &m)
{
int k;
k=l;
l=m;
m=k;
}

No comments:

Post a Comment