Showing posts with label Swapping. Show all posts
Showing posts with label Swapping. Show all posts

Friday, 21 September 2018

Swapping of two values without using the third variable in C++

Swapping of values stored in two variables 


Swapping is the process of exchanging or interchanging the values stored in two variables. Swapping of two number is done by two or three variable methods. This program is done by two variable method.  E.g. a=4 and b=9 then after swapping the values, we get a=9 and b=4.


Write a program in C++ to swap the value of two integer variables by using two variable method.


Firstly we will take two variables as input from the user and store it in a and b. Then we will perform some addition and subtraction to swap the values in the variables as in the example.
E.g.  a=9; b=4;

Action          Result
a=a+b;          a=9+4=13;
b=a-b;           b=13-4=9;
a=a-b;           a=13-9=4;

Final result a=4;  b=9;


Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter first number in a=";
cin>>a;
cout<<"Enter second number in b=";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\nAfter swapping:"<<endl;
cout<<"The value in a="<<a<<endl;
cout<<"The value in b="<<b;
getch();
}



Output


Interchanging the value of two variables without using the third variable
SWAPPING OF TWO VALUES USING TWO VARIABLES











Explanation

In this program, we swapped the values of a and b without using any other variables. After entering the values in a and b we performed addition and subtraction to interchanged the values. Firstly, we added the two numbers and stored in a. Then subtracted a and b and stored the value in b. Finally, we again subtracted the value of a and b and stored in a. Thus we obtained the interchanged values of the two variables.






Swapping of two numbers using three variables in C++

Swapping of two numbers using the third variable




Swapping is the process of exchanging or interchanging the values stored in two variables. Swapping of two number is done by two variable method and three variable method. This program is done by three variable method. E.g. a=5 and b=6 then after swapping the values we get a=6 and b=5.












Write a program in C++ to swap values of two integer variables using the third variable.


We will take two values from the users and store the values in two variables. We will also declare another variable and use it for swapping the two numbers. Finally, we will display the swapped values.


Program



#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,swap;
clrscr();
cout<<"Enter first number in a=";
cin>>a;
cout<<"Enter second number in b=";
cin>>b;
swap=b;
b=a;
a=swap;
cout<<"\nAfter swapping:"<<endl;
cout<<"The new value in a="<<a<<endl;
cout<<"The new value in b="<<b;
getch();
}



    



Output



Swapping of variables
SWAPPING OF TWO VALUES USING THE THIRD VARIABLE










Explanation


In this program, we interchanged the values of two variables (b) using the third variable (swap). We assign the value of a in swap. Then we assigned the value of b to a. Again we assign the value of swap to a. After this, we get the swapped values in variables a and b.