Saturday 20 September 2014

Write a C++ program illustrating Swapping integer values by reference.

#include<iostream.h>
#include<conio.h>
void swap (int &a, int &b)
{
/* &a and &b are reference variables */
int temp;
temp=a;
a=b;
b=temp;
}
main()
{
clrscr();
int i=5,j=10;
cout<<"Before swapping I = "<<i<<" J = "<<j<<endl;
swap(i,j);
cout<<"After swapping I = "<<i<<" J = "<<j<<endl;
}

No comments:

Post a Comment