Swapping means interchanging.

Let's consider two variables x=10 & y=20. After swapping it will become x=20 and y=10.Here x & y value's are interchanged.

   

There are various methods to swap two numbers. Here we are going to discuss some of the methods.

  • Swapping of two numbers with using a third variable.
  • Swapping of two numbers without using third/temp variable.
  • Swapping of two numbers using bit-wise XOR operator.
  • Swapping of two numbers using pointer.
  • Swapping of two numbers using function.

1. Swapping Of Two Numbers With Using a Third Variable.

To swap two numbers using third variable we have to declare a temp variable.

temp=x; //contains of x will be assigned to temp

Again y value we will assign to x .x value will be replaced with y value.

x=y;

Now temp value we will assign to y .

y=temp;

Program:


#include <stdio.h>

void main()
{
int x=10, y=20, temp;

printf("Before Swapping\n x = %d\n y = %d\n", x, y);

temp = x;
x = y;
y = temp;

printf("After Swapping\n x = %d\n y = %d\n", x, y);
}

2. Swapping of two numbers without using third/temp variable.

Swapping can be done also without using a third variable.

x=x+y;//This will modify x=30 & y=20

y=x-y;//This will modify y value i.e y=10 & x=30

x=x-y;//This will modify x value i.e x=20 & y=10

Program:


#include <stdio.h>

void main()
{
int x=10, y=20;

printf("Before Swapping\n x = %d\n y = %d\n", x, y);

x=x+y;
y=x-y;
x=x-y;

printf("After Swapping\n x = %d\n y = %d\n", x, y);
}

3. Swapping of Two Numbers Using Bit-wise XOR Operator.

Using XOR operator we can also swap two numbers.

Let's first understand how the bit-wise XOR operator works.

Let's convert the x & y values to its corresponding binary values

x=10=0000 1010

y=20=0001 0100

Now

x=x^y=(0000 1010)^(0001 0100)=0001 1110//x=30, y=20

y=x^y=(0001 1110)^(0001 0100)=0000 1010//x=30,y=10

x=x^y=(0001 1110)^(0000 1010)=0001 0100//x=20,y=10

Program:


#include <stdio.h>
void main()
{
int x=10, y=20;

printf("Before Swapping\n x = %d\n y = %d\n", x, y);

x=x^y;
y=x^y;
x=x^y;

printf("After Swapping\n x = %d\n y = %d\n", x, y);
}

4. Swapping of Two Numbers Using Pointer


#include <stdio.h>

void main()
{
int x=10, y=20,*ptr1,*ptr2,temp;;

printf("Before Swapping\n x = %d\n y = %d\n", x, y);
ptr1 = &x;
ptr2 = &y;

temp = *ptr2;
*ptr2 = *ptr1;
*ptr1 = temp;

printf("After Swapping\n x = %d\n y = %d\n", x, y);
}

5. Swapping of Two Numbers Using User-Defined Function

Swapping of two numbers in user-defined function is bit tricky we will discuss this concept in call by value & call by reference topic.Here lets write the program only.


#include <stdio.h>

void swap(int*, int*); //function declaration

void main()
{
int x=10, y=20;

printf("Before Swapping\nx = %d y = %d\n", x, y);

swap(&x, &y); //function call

printf("After Swapping\nx = %d y = %d\n", x, y);
}
//function definition
void swap(int *ptr1, int *ptr2)
{
int temp;
temp = *ptr2;
*ptr2 = *ptr1;
*ptr1 = temp;
}

 

That was all about Swapping of two numbers in programming C, Have fun with C Programming!

 

 

 

Comments