Javatpoint Logo
Javatpoint Logo

C++ Program to swap two numbers without third variable

We can swap two numbers without using third variable. There are two common ways to swap two numbers without using third variable:

  1. By + and -
  2. By * and /

Program 1: Using * and /

Let's see a simple C++ example to swap two numbers without using third variable.

Output:

Before swap a= 5 b= 10     
After swap a= 10 b= 5

Program 2: Using + and -

Let's see another example to swap two numbers using + and -.

Output:

Before swap a= 5 b= 10  
 After swap a= 10 b= 5





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA