C++ Program To Swap Two Integers (Two Methods)

C++ Program To Swap Two Integers | C++ for beginners


Method 1 (Using third variable) :

1)We make a new variable 'temp'. 2)Assign the value of b to temp and assign the value of a to b. 3)Then we assign the value of temp to a.

Program :

Output :


Method 2 (Using arithmetic operations) :

The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from the sum.

(Note:For very high values of a,b it can cause arithmetic overflow)

Program :

Output :


Comments