Program to swap two numbers using pass by address.
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y);
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);
cout<<"\nAfter swapping:\n";
cout<<"a="<<a;
cout<<"\nb="<<b;
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
output
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y);
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);
cout<<"\nAfter swapping:\n";
cout<<"a="<<a;
cout<<"\nb="<<b;
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
output
No comments:
Post a Comment