Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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-




 





This post first appeared on Technical Keeda, please read the originial post: here

Share the post

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

×

Subscribe to Technical Keeda

Get updates delivered right to your inbox!

Thank you for your subscription

×