Swapping of Two Numbers in C

Introduction

In this article, we will learn how to write a program for swapping of two numbers in c programming. First of all, we should know, what is the meaning of swapping?

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Swapping is nothing but interchange. For example, we have two variables i.e. a=5 and b=10. After swapping, the values should be a=10 and b=5.

Expected Output

Enter any two numbers
5
10
Before swapping, a=5 and b=10
After swapping, a=10 and b=5

Here, we are going to learn 4 important techniques for swapping of two numbers in c programming. I can increase the number of techniques but some methods like swapping using call by value and call by reference will be covered in separate articles.

Also Read: C Language Program to Count the Number of Lowercase Letters in a Text File

Let’s see these techniques one by one.

Swapping of two numbers in C using third variable

In this technique, I am using third variable. Just imagine, you and your friend need to interchange yours rooms. If both the rooms are filled with so much stuff, then what will you do? The answer is so simple. You have to empty one room by shifting the stuff into third room or a empty space. In this way, one room is vacant now. Now, shift the stuff from second room to first room and after that, shift the stuff from empty area to the second room.

In a similar way, we need to write a program in c for swapping of two numbers using a third variable.

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int a,b,temp=0;
  printf("Enter any two numbers\n");
  scanf("%d%d",&a,&b);
  printf("\nBefore swapping, a=% and b=%d",a,b);
  temp=a;
  a=b;
  b=temp;
  printf("\nAfter swapping, a=% and b=%d",a,b);
  return 0;
}

In the above c program, you can see swapping of two numbers using third variable. The following code is used for swapping.

temp=a;
a=b;
b=temp;

We have used three variables here and those variables are a, b and temp. The variable temp is the temporary variable. This is just like blank room or area in the above example.

Also Read: Perfect Number in C Programming using All Loops

For example, suppose the value of a=5 and b=10.

The first statement is temp=a. The value of a is assigned to the variable temp. Now, the value of temp is 5 because the value of a is 5.

The second statement is a=b. Here, the value of b is assigned to the value of a. That means, the current value of a is 10.

Now, it’s time to change the value of b. So, here comes the third statement b=temp. In the first statement, the value of temp is 5 and now that value is assigned to the value of b. Therefore, the current value of b is 5. In this way, we have successfully interchange the values of two variables.

Now, let’s move to the next technique or method.

Swapping of two numbers in C without using a third variable (+ and -)

This is a completely different method. In this program, we have a restriction not to use the third variable. Then, how can we do this program? Don’t worry. We have a very interesting solution to this. Let’s see the program first.

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int a,b;
  printf("Enter any two numbers\n");
  scanf("%d%d",&a,&b);
  printf("\nBefore swapping, a=% and b=%d",a,b);
  a=a+b;
  b=a-b;
  a=a-b;
  printf("\nAfter swapping, a=% and b=%d",a,b);
  return 0;
}

As you can see, output of the program remains same. Only the processing part is different. Here, we are not using third variable or temporary variable. Then what we have done? See the following code.

Also Read: Switch Case in C Programming

a=a+b;
b=a-b;
a=a-b;

Here, we have written a program in c for swapping of two numbers using arithmetic operators + and -. Here, we have to add the values of a and b and store the result in the variable a. In the second step, subtract the value of b from a and do the same thing in the third step also.

For example, suppose, the value of a is 5 and the value of b is 10. So, the first statement i.e. a=a+b means the value of a becomes 15. This value is greater than the value of b. Now, the second statement is subtracting the value of b from a. Therefore, the latest value of b is 15-10 is 5. Now, we need to again subtract the value of b from a. But, this time take the value of b is 5, not 10. Therefore, the latest value of a is 15-5=10. So, we have successfully, interchange the value of two variables without using a third variable.

Swapping of two numbers in C without using a third variable (using * and /)

This is also a completely different method. In this program, we are not going to use temporary variable. Here, we are using arithmetic operators * and / for swapping of two numbers. See the following program.

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int a,b;
  printf("Enter any two numbers\n");
  scanf("%d%d",&a,&b);
  printf("\nBefore swapping, a=% and b=%d",a,b);
  a=a*b;
  b=a/b;
  a=a/b;
  printf("\nAfter swapping, a=% and b=%d",a,b);
  return 0;
}

You can observe all the programs, only the processing part is changing and the remaining part is the same. So, see the following processing part.

a=a*b;
b=a/b;
a=a/b;

Here, we have written a program in c for swapping of two numbers using arithmetic operators * and /. We know, * is used for multiplication and / is for division operation. Here, we have to multiply the values of a and b and store the result in the variable a. In the second step, divide the value of a by b and do the same thing in the third step also.

Also Read: Strong Number in C Programming

Suppose, the value of a is 5 and the value of b is 10. So, the first statement i.e. a=a*b means the value of a becomes 50. This value is greater than the value of b. Now, in the second statement, we are dividing a by b and storing that result in b. So, the value of b is 50/10 and the latest value of b is 5. Now, we need to again divide the value of a by b. But, this time take the value of b is 5, not 10. Therefore, the latest value of a is 50/5=10. So, we have successfully, interchanged the value of two variables without using a third variable.

I hope, you are getting what I am trying to say. We have also other methods for swapping of two numbers in c programming.

Swapping of two numbers in C using bitwise XOR operator.

Again and again, I am telling from the beginning that only the processing part is different. Here also, you can different processing part. But, before that let me tell you the use of bitwise XOR operator. This is a logical operators and it works on single bit only, therefore, we are saying this is bitwise XOR operator.

Also Read: Operators in C with Detailed Explanation

It is a binary operator that means it works on two operands and here, its input will be 0 and 1. Whatever numbers we will enter, this operator will perform bits of that numbers. This operator returns 0 when all the operands are the same. That means, for similar like (0^0 or 1^1) bits, it will produce the result 0 and for the different bits like (0^1 or 1^0), it will give the result 1. I am not going in very much details in operators in c programming.

Now, see the following code in c programming.

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int a,b;
  printf("Enter any two numbers\n");
  scanf("%d%d",&a,&b);
  printf("\nBefore swapping, a=% and b=%d",a,b);
  a=a^b;
  b=a^b;
  a=a^b;
  printf("\nAfter swapping, a=% and b=%d",a,b);
  return 0;
}

So, the processing part is shown below.

a=a^b;
b=a^b;
a=a^b;

Again, take the same value of a is 5 and b is 10. In the first statement, we are XORing the value of a and b and storing the result in the variable a. Now, 5^10 is 15. Now, the latest value of a is 15. In the second statement, the value of b is 15^10 is 5. Now, the value of b is changed from 10 to 5. In the third statement, the value of a is 15^5 is 10. So, you can see, both values of the variables a and b have been changed.

XOR Calculator Online

I hope, you are enjoying these small but interesting methods for swapping of two numbers in c programming.

Thanks for reading.

Leave a Comment