-->

10/16/20

How to swap two variable without using third variable in cpp easy explanation

 How to swap two variable without using the third variable in cpp-

this question  How to swap two variables without using a third variable is asked much time in a face-to-face interviews or some coding round too. So it's an important question for beginners. Though it is an easy question, such types of questions improve your thinking ability.

Recommended try this question before jumping to a solution.

How to swap two variable without using third variable in cpp


Solution-

while swapping two integer without using third variable you need a little maths logic.
step 1- add both of integer and assign them into a.
a=a+b;
step 2- now subract a from b from a and assign it into b.
b=a-b;
step 3- and now subtract b from a and assign it into a
a=a-b;

this is how you can swap two variable without using third variable.

Code mentioned below-




 



Advertiser

all right reserved @technicalkeeda.in. Powered by Blogger.