Signup/Sign In
LAST UPDATED ON: SEPTEMBER 17, 2024

Program of Swapping two numbers in C

Swapping of two numbers in C Language is the process in which the value of two variables is exchanged using some code. For example,

a = 5, b = 4
// After swapping:
a = 4, b = 5

We can swap two numbers in various ways as follows:

  1. Swapping two variable values using a Temporary Variable

  2. Swapping two variable values using Addition and Subtraction

  3. Swapping two variable values using Bitwise Operator

  4. Swapping two variable values using Multiplication and Division

1. Swapping two numbers in C using a temporary Variable

Let's start with the algorithm steps first,

Algorithm:

  1. declare three variables x, y and temp
  2. take input in x and y, say x = 5 and y = 7
  3. assign the value of x to temp, say 5
  4. now temp = 5 and x = 5
  5. put the value of y in x, so y = 7 and x = 7
  6. then, put the value of temp in y, so temp = 5 and y = 5

Below is a program to swap two numbers using temporary variable.

#include<stdio.h>
#include<conio.h>

void main()
{
    int x = 10, y = 15, temp;
    temp = x;
    x = y;
    y = temp;
    printf("x = %d and y = %d", x, y);
    getch();
}

x = 15 and y = 10


2. Swapping two numbers using Addition and Subtraction

Let's start with the algorithm steps first,

Algorithm:

  1. Take input of the two numbers, say x = 5 and y = 7
  2. Store the sum of both the numbers in the first number(x = 5 + 7 so x = 12) and store the difference of both the numbers in the second number(y = 12 - 7, so y = 5).
  3. Then store their difference in the first number(x = 12 - 5 so x = 7) and print.

Below is a program to swap two numbers without using any temporary variable, and use addition and subtraction operator for doing it.

#include<stdio.h>
#include<conio.h>

void main()
{
    int x = 10, y = 15;
    x = x + y - (y = x);
    printf("x = %d and y = %d",x,y);
    getch();
}

x = 15 and y = 10


3. Swapping two numbers using Bitwise Operator

XOR gives output as 1 when two different bits are XORed and give 0 when two same bits are XORed. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ. For example, XOR of 7 (0111) and 5 (0101) is (0010).

Algorithm:

  1. Input two numbers, say x = 5 and y = 7
  2. XOR the two numbers and store the result in the first number(x = 5 ^ 7 so x = 2)
  3. XOR the two numbers again and store the result in the second number (y = 2 ^ 7 so y = 5)
  4. XOR the two numbers again and store the result in the first number (x = 2 ^ 5 so x = 7)

Below is the program to swap two numbers using bitwise operator.

#include<stdio.h>
#include<conio.h>

void main()
{
    int x = 6, y = 4;
    x = x^y;
    y = x^y;
    x = x^y;
    printf("x = %d and y = %d", x, y);
    getch();
}

x = 4 and y = 6


4. Swapping two numbers using Multiplication and Division

Let's start with the algorithm steps first,

Algorithm:

  1. Take input of the two numbers, say x = 5 and y = 7
  2. Store the product of both the numbers in the first number(x = 5 * 7 so x = 35) and store the quotient of both the numbers in the second number(y = 35 / 7, so y = 5).
  3. Then store their difference in the first number(x = 35 / 5 so x = 7) and print.

Below is the program to swap two numbers using multiplication and division.

#include<stdio.h>
#include<conio.h>

void main()
{
    int x = 6, y = 4;
    x = x*y;
    y = x/y;
    x = x/y;
    printf("x = %d and y = %d", x, y);
    getch();
}

x = 4 and y = 6


Points to Remember

  1. In the algorithm using addition and division and XOR, if the values are very big, it can result in integer overflow.
  2. In the algorithm using division and multiplication, if one of the values is zero, the product will become zero and the algorithm will fail.