Swap Two Numbers using a Third Variable

Write a program to swap two numbers using a third variable. This question can also be written as write a program to swap two numbers using a temp variable. n my earlier post

This is the easy programming question for beginners.

Swap two numbers without using a third variable.

Swap two numbers using a pointer.

Swap two numbers using bitwise XOR operator.

Subscribe Our Tutorials

Get Latest Updates on Facebook

C Program to Swap Two Numbers using a Third Variable.

In swapping, we need to exchange the position of two elements using a third variable. Let’s check the logic of this program.

Logic

1. Declare three variables.
2. Assign the value of the first variable in temp. Then assign the value of the second variable  into the first variable. Finally, assign the value of temp variable into the second variable.

Suppose we have declared three variables temp, a and b. Let’s see how the position of two variables a and b is exchanged (Swap).

C, C++ Programming Questions for Practice

PHP Program to Swap Two Numbers using a Third Variable

WebRewrite

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.