C Program to Swap Two Numbers Without Third Variable

C program to swap two numbers without using third variable can be designed by using basic arithmetic operators like +, -, *, / or bit-wise operator like X-OR. Swapping of two numbers means the exchanging of values between them.

There are three different approaches for swapping without using a third variable:

Method 1. Use the addition(+) and subtraction(-) operators

Method 2. Use the division(/) and multiplication(*) operators

Method 3. Use the X-OR operator

The method 1 and 2 have been shown below. Check here the flowchart of the program.







1. Algorithm of Method 1 (use aritmatic operators + and -) to swap two Numbers Without Third Variable


1. Take two integers say $x,~y$ as input.

2. Compute $x= x+y$

3. Compute $y= x-y$

4. Compute $x= x-y$

5. Return $x, y$




2. Algorithm of Method 2 (use aritmatic operators * and /) to swap two Numbers Without Third Variable


1. Take two integers say $x,~y$ as input.

2. Compute $x= x*y$

3. Compute $y= x/y$

4. Compute $x= x/y$

5. Return $x, y$




3. Pseudocode of Method 1 (use aritmatic operators + and -) to swap two Numbers Without Third Variable


Input : Two integer numbers $x,~y$

Output : $x,~y$ with exchanged values

1. Procedure swap($x, y$):

2. $x \leftarrow x + y$

3. $y \leftarrow x - y$

4. $x \leftarrow x - y$

5. Return $x,~y$

6. End Procedure





4. Pseudocode of Method 2 (use aritmatic operators * and /) to swap two Numbers Without Third Variable


Input : Two integer numbers $x,~y$

Output : $x,~y$ with exchanged values

1. Procedure swap($x, y$):

2. $x \leftarrow x * y$

3. $y \leftarrow \frac{x}{y}$

4. $x \leftarrow \frac{x}{y}$

5. Return $x,~y$

6. End Procedure





5. Time complexity of Method 1, 2 to Swap Two Numbers Without Third Variable


Time Complexity: O(1)





6. C Program & output to Swap Two Numbers Without Third Variable (Using Addition and Subtraction)

Code has been copied
/**********************************************
            alphabetacoder.com
C program to swap two numbers without third
variable but using arithmetic operators + and -
***********************************************/
#include <stdio.h>

int main() {
    int num1, num2;
    // take input of two numbers 
    printf("Enter two numbers = ");
    scanf("%d%d", & num1, & num2);
    printf("Before swapping: number1 = %d and number2 = %d\n", num1, num2);

    //do swapping using + and -
    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;

    printf("After swapping: number1 = %d and number2 = %d", num1, num2);
    return 0;
}

Output


Enter two numbers = 5 10

Before swapping: number1 = 5 and number2 = 10

After swapping: number1 = 10 and number2 = 5




7. C Program & output to Swap Two Numbers Without Third Variable (Using Multiplication and Division)

Code has been copied
/*********************************************
            alphabetacoder.com
C program to swap two numbers without third
variable but using arithmetic operators * and /
**********************************************/
#include <stdio.h>

int main() {
    int num1, num2;
    // take input of two numbers 
    printf("Enter two numbers = ");
    scanf("%d%d", & num1, & num2);
    printf("Before swapping: number1 = %d and number2 = %d\n", num1, num2);

    //do swapping using * and /
    num1 = num1 * num2;
    num2 = num1 / num2;
    num1 = num1 / num2;

    printf("After swapping: number1=%d and number2 = %d", num1, num2);
    return 0;
}

Output


Enter two numbers = 5 10

Before swapping: number1 = 5 and number2 = 10

After swapping: number1 = 10 and number2 = 5