Author : @aarsoftx aarsoftx

Posted on Sat, Dec 17 2022

How to swap data of variables in Two Methods

This is a tutorial post about swapping variables in two methods.

Article on C++

How to swap data of variables in Two Methods

In most cases in programming we need variable swapping in out program. Swapping variables mean two variable with exchage there values with each others. In this post you can learn how to do this in 2 different methods.

The methods are : 

  1. With third variable
  2. Without third variable

Method 1 : (With third variable)

In this method you need to store one of two variables data and assign it to other one. In this case I am using "c" as third variable here. 

Method 2 : ( Without third variable)

In this method you don't need any thrid variable for swaping but you need sum simple math. At first we need to make sum of two variables and assign it to first variable. Then subtract value of second variable from first variable and assign it to second variable. The again subtract second variable from first variable and assign it to first variable. After doing that swapping operation should be done.

There you go. By using mehtod above you can swap variables easily. Have a good Day.