Given two integer numbers and we have to swap them.
We are swapping numbers using two methods:
1) Swapping using third variable
To swap numbers, we use a temporary variable to holds the value, firstly we assign first variable to the temporary variable then assign second variable to the first variablecand finally assigns value which is in temporary variable (which holds first number) to the second variable.
Consider the program:
Output
2) Swapping without using third variable
Here, we do not use any extra variable to swap the numbers. There are some set of statements (with mathematical operations which performs on the numbers), which swaps the values of variable which are using in these operations.
Example: If we have to swap the values of variable a and b, then the set of statements to swap them, are:
Consider the program:
Output