Saturday, February 14, 2015

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<<"
Befor swapping:
a="<<a<<" b="<<b;

swap(a,b);
cout<<"

After swapping:
a="<<a<<" b="<<b;

getch();
}

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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.