Swap two numbers without Using third variable in C
In this program we will swap two numbers without using third variable. Lets understand common ways to swap two numbers without using third variable:
- By + and -
- By * and /
First we will learn swapping of two numbers
Program 1: Using Third Variable
Let's write a program in C to swap two numbers using third variable.
Output:
Before swap a=10 b=20
After swap a=20 b=10
Before swap a=10 b=20 After swap a=20 b=10
Program 1: Using + and -
Let's write a program in C to swap two numbers without using third variable.
Output:
Before swap a=10 b=20 After swap a=20 b=10
Program 2: Using * and /
Let's see another example to swap two numbers using * and /.
Output:
Before swap a=10 b=20 After swap a=20 b=10
EmoticonEmoticon