Program to swap two numbers in C++
Program to swap two numbers in C++
It is a pretty basic program written in C++ to swap (exchange) the two numbers etc.
Program :-
#include<iostream.h>
void swap(int*,int*);
void main()
{
int a,b,tmp;
cout<<"Enter the value of variable a :-";
cin>>a;
cout<<"Enter the value of variable b :-"
cin>>b;
cout<<"values of variables before function call :- ";
cout<<a<<"\t"<<b;
swap(&a,&b);
cout<<"\n values of variables after function call :- ";
cout<<a<<"\t"<<b;
}
void swap(int*m,int*n)
{
int tmp;
tmp =*m;
*m=*n;
*n=tmp;
}
Output :-
Enter the value of variable a:- 2
Enter the value of variable b:-3
values of variables before function call:- 2 3
values of variables after function call :-3 2
It is a pretty basic program written in C++ to swap (exchange) the two numbers etc.
Program :-
#include<iostream.h>
void swap(int*,int*);
void main()
{
int a,b,tmp;
cout<<"Enter the value of variable a :-";
cin>>a;
cout<<"Enter the value of variable b :-"
cin>>b;
cout<<"values of variables before function call :- ";
cout<<a<<"\t"<<b;
swap(&a,&b);
cout<<"\n values of variables after function call :- ";
cout<<a<<"\t"<<b;
}
void swap(int*m,int*n)
{
int tmp;
tmp =*m;
*m=*n;
*n=tmp;
}
Output :-
Enter the value of variable a:- 2
Enter the value of variable b:-3
values of variables before function call:- 2 3
values of variables after function call :-3 2
Comments
Post a Comment