C++ Program to Swap two numbers

 C++ Program to Swap two numbers:

Algorithm:

START
   Var1, Var2, Temp
   Step 1  Copy value of Var1 to Temp
   Step 2  Copy value of Var2 to Var1
   Step 3  Copy value of Temp to Var2
STOP

Code:

#include<iostream>           //include libraries
using namespace std;
int main()
{
    int a,b;                            // take 2 var
    cout<<"before swapping"<<endl;            //display values before swap
cout<<"a =";
cin>>a;
cout<<"b =";
cin>>b;
a=a+b;                                        //procedure to swap
b=a-b;
a=a-b;
cout<<"After swapping";               //display values after swap
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;

system("pause");
return 0;

}

Output:






Comments

Popular posts from this blog

Loops and Functions Session 8

Python Installation

Variable & Data Types (Session 3)