- C++

C++ Program to Swap two numbers.

 

“Swapping of integers is defined as the variables that are interchanged after swapping the variables,called as Swapping of integers”

Example: If the input is given as a=5 and b=4 (before Swapping)
The output will be as a=4 and b=5 (after Swapping).
                Input:
a=20 and b=40, before swapping.
Output:
a=40 and b=20, after swapping.

Sample Code for Swapping of integers is found here:

#include <iostream>            //Header file.
using namespace std;
void swap(int,int);
main()
{
   int a,b;
   cout<<"Enter two numbers";
   cin>>a>>b;
   cout<<"Before swap a="<<a<<",b="<<b;
  swap(a,b);                                                    //result before swapping
}
void swap(int a,int b)
{
   a=a+b;
   b=a-b;
   a=a-b;
   cout<<"After swap a="<<a<<",b="<<b;       //result after swapping
}

Leave a Reply