C Program to Swap Two Numbers

Here we’ll discuss about different techniques how we can swap two numbers. Swapping two numbers means exchanging the values of two variables. For example, if value of A is 5 and value of B is 10, then after swapping values of A and B would be 10 and 5 respectively.

Swap Two Numbers Using Third Temporary Number

We can swap two numbers with the help of another temporary number.

Logic

  1. Copy the value of first variable to a temporary variable.
  2. Assign the second variable to the first variable.
  3. Assign the temporary variable to the second variable.

Program

/*
 * File name: test.c
 * Compile: cc test.c -o test
 * */
#include <stdio.h>

void main()
{
  unsigned int A, B, tmp;

  printf("Enter the first variable (A): ");
  scanf("%d", &A);
  
  printf("Enter the second variable (B): ");
  scanf("%d", &B);
  
  printf("\nBefore swapping:\n");
  printf("A = %u, B = %u\n", A, B);
  
  /*Swapping A and B*/
  tmp = A;
  A = B;
  B = tmp;
  
  printf("Before swapping:\n");
  printf("A = %u, B = %u\n", A, B);
}

This program first takes input of two variables A and B and prints them before swapping. Then it swaps them using the above logic and prints them again after swapping.

Here is the output.

Enter the first variable (A): 32
Enter the second variable (B): 45

Before swapping:
A = 32, B = 45
Before swapping:
A = 45, B = 3
2

Using Function Call

In the above example swapping logic is implemented in the main() itself. Here we’ll see how we can write a function that will swap the input arguments. To do that, we have to pass the arguments as reference instead of value.

Program

/*
 * File name: test.c
 * Compile: cc test.c -o test
 * */
#include <stdio.h>

void swap(unsigned int *a, unsigned int *b) {
  unsigned int tmp;
  
  tmp = *a;
  *a = *b;
  *b = tmp;
}

void main()
{
  unsigned int A, B, tmp;

  printf("Enter the first variable (A): ");
  scanf("%d", &A);
  
  printf("Enter the second variable (B): ");
  scanf("%d", &B);
  
  printf("\nBefore swapping:\n");
  printf("A = %u, B = %u\n", A, B);
  
  /*Swapping A and B by calling a function swap()*/
  swap(&A, &B);
  
  printf("Before swapping:\n");
  printf("A = %u, B = %u\n", A, B);
}

Here we called the function swap() with references of A and B. Output would be same here.

Swap two Variables Without Using Third One

In the above examples, we took help of a third temporary variable. Here we’ll see how we can achieve the same without using a third variable.

Logic

  1. Add two variables and assign the summation to the first variable.
  2. Subtract the second variable from the first one and assign the result to the second variable. In this step second variable gets the value of first one.
  3. Subtract the second variable from the first one and assign the result to the first variable. In this step first variable gets the value of second one.

Program

/*
 * File name: test.c
 * Compile: cc test.c -o test
 * */
#include <stdio.h>

void main()
{
  unsigned int A, B, tmp;

  printf("Enter the first variable (A): ");
  scanf("%d", &A);
  
  printf("Enter the second variable (B): ");
  scanf("%d", &B);
  
  printf("\nBefore swapping:\n");
  printf("A = %u, B = %u\n", A, B);
  
  /*Swapping A and B */
  A = A + B;
  B = A - B;
  A = A - B;
  
  printf("Before swapping:\n");
  printf("A = %u, B = %u\n", A, B);
}

Here the swapping logic does not use a third variable. It works in most cases. But it has some problem.

Problem with This Logic

But consider the following output.

Enter the first variable (A): 4294967001
Enter the second variable (B): 4294967100

Before swapping:
A = 4294967001, B = 4294967100
Before swapping:
A = 4294967001, B = 99

From this output we can see that numbers are not swapped properly. When we added two variables (A+B), the result became 8589934101. But an unsigned integer can not store a value bigger than 4294967295. So when we assigned the summation to A, the information is lost. After that we could not recover the original values.

Solutions

We solve the above problem in various ways. We can slightly change this 3 line snippet

  A = A + B;
  B = A - B;
  A = A - B;

to

A = A + B - (B = A);

Here we are not storing the summation to any variable. (B=A) will assign A to B and then subtracted from the summation. Intermediate results of the right side of the expression will be stored in registers. The final result will be stored in variable A.

If we run this problem, the output would be changed like this.

Enter the first variable (A): 4294967001
Enter the second variable (B): 4294967100

Before swapping:
A = 4294967001, B = 4294967100
Before swapping:
A = 4294967100, B = 4294967001

We can also solve this problem with XOR (Exclusive OR) operation.

Swapping Two Numbers using XOR Operation

The logic is very similar to the summation one. Here is the program.

/*
 * File name: test.c
 * Compile: cc test.c -o test
 * */
#include <stdio.h>

void main()
{
  unsigned int A, B, tmp;

  printf("Enter the first variable (A): ");
  scanf("%d", &A);
  
  printf("Enter the second variable (B): ");
  scanf("%d", &B);
  
  printf("\nBefore swapping:\n");
  printf("A = %u, B = %u\n", A, B);
  
  /*Swapping A and B */
  A = A ^ B;
  B = A ^ B;
  A = A ^ B;
  
  printf("Before swapping:\n");
  printf("A = %u, B = %u\n", A, B);
}

One integer is enough to store the result of XOR Operation of two integer. So there won’t be any overflow problem.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
0