C++ Program to Swap two Numbers without using Third Variable

To write the c++ program to swap two numbers without using third variable we need to use the concept of call by reference.

Basically in call by reference, instead of passing the value of the variable, we pass its address or reference to another function.

This can be done in two ways

  1. Creating alias of the variable in another function (Can be called as call by Reference).
  2. By passing address or reference.

C++ Program to Swap two Numbers by Creating Alias

C++ Program to Swap two Numbers by Passing Address

In this method we are using pointers because pointers can store address of the variable and can be used in another function by derefrencing it.

Also check:

C++ programs.

C Programs.

Python Programs.


I am a B.tech IT Student from India. Technology and programming is the most enthusiastic thing for me in this world. I like learning new techniques and use them for real-world application because I feel tech is future. Let's learn and grow TOGETHER.