Program to swap two numbers in C++

Using Visual Studio  2015


===============================================================


#include<iostream>
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

Popular posts from this blog

Suppose you are given a square matrix of size n x n, write a program to determine if this is an identity matrix. 2D array

Program to create a multiplication table of a given number using while loop.

Program to find equal values using if statement in C++