Program to swap two numbers in C++
Using Visual Studio 2015
===============================================================
#include<iostream>
using namespace std;
#include<conio.h>
using namespace std;
#include<conio.h>
void main()
{
{
int a,b,c;
cout<<"A=";
cin>>a;
cout<<"B=";
cin>>b;
c=a;
a=b;
b=c;
cout<<"\nA="<<a;
cout<<"\nB="<<b;
_getche();
}
}
=========================================================================
It is an easy program. First we take two input numbers from the user. Then we assign first number to the variable 'c'. Then we assign the value of 'b' to the variable 'a'. In this way, 'c' has the value of 'a' and 'a' has the value of 'b'. Then we assign the value of 'c' (which was the value of 'a') to 'b'. Then we store new values assigned to 'a' and 'b' using cout.
Comments
Post a Comment