C Program to Swap Two Numbers

How to write a C program to swap two numbers using a temporary variable and also without using a temporary or third variable?. For this C Program to Swap Two Numbers purpose, we are going to use Pointers, Functions, Arithmetic Operators, Bitwise Operators, Call By Reference concepts.

C Program to Swap Two Numbers

This program allows the user to enter two integer values. By using the third variable, this C program Swap two numbers.

/* C Program to Swap Two Numbers */

#include <stdio.h>

int main()
{
  int a, b, Temp;
 
  printf("\nPlease Enter the value of a and b\n");
  scanf("%d %d", &a, &b);
 
  printf("\nBefore Swap: a = %d and b = %d\n", a, b);
 
  Temp = a;
  a    = b;
  b    = Temp;
 
  printf("\nAfter Swapping: a = %d and b = %d\n", a, b);
 
  return 0;
}

C swap two numbers using a temp variable output

Please Enter the value of a and b
6
13

Before Swap: a = 6 and b = 13

After Swapping: a = 13 and b = 6

Within this Program to Swap Two Numbers, we assigned a = 6 and b = 13

Temp = a – it means assigning the a value to the Temp variable
Temp = 6

a = b – it means assigning the b value to the variable a
a = 13

b = Temp – it means assigning the Temp value to the variable b
b = 6

The last C Programming Printf statement will print the a, b values as output.

C Program to Swap Two Numbers Using Pointers

This program allows the user to enter two integer values. This C program uses the Pointers concept to swap two numbers. Within the main Function of this program, we are using the third variable Temp to Swap those two numbers.

/* C Program to Swap Two Numbers Using Pointers */

#include <stdio.h>

int main()
{
  int a, b, *i, *j, Temp;
 
  printf("\nPlease Enter the value of a and b\n");
  scanf("%d %d", &a, &b);
 
  printf("\nBefore Swap: a = %d  b = %d\n", a, b);
  i = &a;
  j = &b;
  
  Temp = *i;
  *i    = *j;
  *j    = Temp;
 
  printf("\nAfter Swapping: a = %d  b = %d\n", a, b);
  printf("\nAfter Swapping: i = %d  j = %d\n", *i, *j);

  return 0;
}
C Program to Swap Two Numbers Using Pointers

Within this C Program to Swap Two Numbers, the below statements will assign the address of the variables a and b to the pointer variables i and j addresses

  i = &a;
  j = &b;

Below printf statement will display the values inside the variables a and b

printf("\nAfter Swapping: a = %d b = %d\n", a, b);

Below printf statement will display the values inside the pointer variables i and j

printf("\nAfter Swapping: i = %d  j = %d\n", *i, *j);

C Program to Swap Two Numbers Using Functions

This program allows the user to enter two integer values. By using the Call By Value concept in Functions, this program will Swap two numbers.

Within the Function Swap, we are using the third variable Temp to Swap two numbers in c.

/* C Program to Swap Two Numbers Using Functions */

#include <stdio.h>
 
void Swap(int, int);
 
int main()
{
  int A, B;
 
  printf("\nPlease Enter the value of A and B\n");
  scanf("%d %d", &A, &B);
 
  printf("\nBefore Swap: A = %d and A = %d\n", A, B);
 
  Swap(A, B); 
 
  return 0;
}
 
void Swap(int A, int B)
{
  int Temp;
 
  Temp = A;
  A = B;
  B = Temp;   
  
  printf("\nAfter Swapping: A = %d and B = %d\n", A, B);
}
Please Enter the value of A and B
15
25

Before Swap: A = 15 and A = 25

After Swapping: A = 25 and B = 15

In this Program to Swap Two Numbers, when the compiler reaches the Swap(A, B); then, the compiler will automatically jump to the corresponding function

void Swap(int A, int B)

The working procedure of this function is the same as the above program. If you don’t understand the steps, please refer to the C Program to Swap Two Number Analysis part.

Program to Swap Two Numbers Using Call By Reference

This program allows the user to enter two integer values. By using the Functions Call By Reference concept (or Pointers concept), this program Swaps two numbers. Within the Function Swap, we are using the third variable Temp to Swap two numbers.

Please Refer Call By Reference in C Example in PASSING PARAMETER TO THE FUNCTION article to see the functionality.

C Program to Swap Two Numbers Without Using Temp Variable

In this C program, Instead of using the temp or third variable to swap two numbers We are going to use Arithmetic Operators.

/* C Program to Swap Two Numbers Without Using Temp Variable */
#include <stdio.h>

int main()
{
  int a, b;
 
  printf("\nPlease Enter the value of a and b\n");
  scanf("%d %d", &a, &b);
 
  printf("\nBefore Swap: a = %d  b = %d\n", a, b);
 
  a = a + b;
  b = a - b; 
  a = a - b;
 
  printf("\nAfter Swapping: a = %d  b = %d\n", a, b);
 
  return 0;
}

C swap two numbers without using a temp variable output

Please Enter the value of a and b
20
10

Before Swap: a = 20  b = 10

After Swapping: a = 10  b = 20

In this C Swap Two Numbers example, User Entered Values are a =20 and b = 10

a = a + b;
a = 20 + 10 = 30

b = a – b;
b = 30 -10 = 20

a = a – b;
a = 30 – 20 = 10
Final Values after swapping 2 numbers: a = 10 and b = 20

NOTE: Although we can swap 2 numbers in c using multiplication and division approach. However, they may produce strange values if we are working with larger integer values or if any of the integer value is 0

C Program to Swap Two Numbers Using Bitwise OR Operator

In this swap two number program, Instead of using the temp or third variable to swap 2 numbers We are going to use Bitwise Operators.

/* C Program to Swap Two Numbers Using Bitwise OR Operator */
#include <stdio.h>

int main()
{
  int a, b;
 
  printf("\nPlease Enter the value of a and b\n");
  scanf("%d %d", &a, &b);
 
  printf("\nBefore Swap: a = %d  b = %d\n", a, b);
 
  a = a ^ b;
  b = a ^ b; 
  a = a ^ b;
 
  printf("\nAfter Swapping: a = %d  b = %d\n", a, b);
 
  return 0;
}
Please Enter the value of a and b
8
4

Before Swap: a = 8  b = 4

After Swapping: a = 4  b = 8

Within this program to Swap Two Numbers, The user Entered Values are a = 8 and b = 4. The binary values of 8 = 1000 and 4 = 0010.

a = a ^ b = 1000 ^ 0010
a = 1010

b = a ^ b = 1010 ^ 0010
b = 1000

a = a ^ b = 1010 ^ 1000
a = 0010

Final Values after swapping 2 numbers: a = 4 and b = 8