C++ PROGRAMS
Swapping is a very important and useful program that is frequently used in advanced programming. In this, any two numbers are entered by the user and they get swapped and output is the swapped arrangement of the numbers.
For example:
User enters: 12 13
Output: 13 12
In this I will use an user-defined-function for swapping and use three variables - two for the numbers entered and the third one will help in swapping the values.
Code:
#include<iostream.h>
#include<conio.h>
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
cout<<a<<" "<<b;
}
void main()
{
int a,b;
cin>>a>>b;
swap(a,b);
getch();
}
Output:
First Run:
Second Run:
It's interesting that Swapping can also be done without using a third variable. To know how click on Swapping Without Third Variable.
For any queries or suggestions please comment !
No comments:
Post a Comment