Introduction:
In this article I have explained How to swap/interchange two numbers/values in C++ Language. In this article I have
temporary variable to swap numbers.
Implementation: Let's create a simple program to swap two
numbers.
#include<iostream.h>
void
main()
{
clrscr();
int
a,b,temp;
cout<<"Enter
first number= ";
cin>>a;
cout<<"Enter
second number= ";
cin>>b;
cout<<"\nValue
of first number before swapping= "<<a;
cout<<"\nValue
of second number before swapping= "<<b;
temp=a;
a=b;
b=temp;
cout<<"\nValue
of first number after swapping= "<<a;
cout<<"\nValue
of second number after swapping= "<<b;
getch();
}
Run the program using ctrl+F9
Output:
Enter
first number= 2
Enter
second number= 5
Value of first number before swapping= 2
Value of second number before swapping= 5
Value of first number after swapping= 5
Value of second number after swapping= 2
0 comments:
Post a Comment