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, Bitwise Operators, and Call By Reference concepts.

C Program to Swap Two Numbers using temp variable

This program allows the user to enter two integer values. By using the third temp or temporary variable, this C program will 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 : a = %d and b = %d\n", a, b);
 
  Temp = a;
  a    = b;
  b    = Temp;
 
  printf("\nAfter   : a = %d and b = %d\n", a, b);
 
  return 0;
}
C Program to Swap Two Numbers using Temp Variable

Within this, 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 and b values as output.

C Program to Swap Two Numbers Using Pointers

This program allows the user to enter two integer values. This program uses the Pointers concept to swap two numbers.

Within this C Program to Swap Two Numbers, the first two statements ( i = &a and j = &b) will assign the address of the variables a and b to the pointer variables i and j addresses.

Next, in this program, we are using the third variable, Temp, to Swap those two numbers. The following print statements, I mean the first one will display the values inside the variables a and b. And the second one will display the values inside the pointer variables i and j

/* 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

C Program to Swap Two Numbers Using Functions

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

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

#include <stdio.h>
 
void CalSw(int, int);
 
int main()
{
  int A, B;
 
  printf("\nPlease Enter the value of A and B\n");
  scanf("%d %d", &A, &B);
 
  printf("\nBefore : A = %d and A = %d\n", A, B);
 
  CalSw(A, B); 
 
  return 0;
}
 
void CalSw(int A, int B)
{
  int Temp;
 
  Temp = A;
  A = B;
  B = Temp;   
  
  printf("\nAfter : A = %d and B = %d\n", A, B);
}
C Program to Swap Two Numbers using Functions

In this example, when the compiler reaches the CalSw(A, B), then the compiler will automatically jump to the corresponding function.

void CalSw(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 above analysis part.

C 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 C program will Swap two numbers. Within the Function, we are using the third variable, Temp.

Please Refer to the Call By Reference Example in Passing Parameter to the Function article to see the functionality.

C Program to Swap Two Numbers without using Temp Variable

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

#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 : a = %d  b = %d\n", a, b);
 
  a = a+b;
  b = a-b; 
  a = a-b;
 
  printf("\nAfter   : a = %d  b = %d\n", a, b);
 
  return 0;
C Program to Swap Two Numbers without Temp Variable

In this interchange, Two Numbers without using the third variable example program, User Entered Values, are a =20 and b = 10

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

b = a-b = 30 -10 = 20

a = a-b = 30 – 20 = 10
Final Values after interchanging the two numbers: a = 10 and b = 20

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

C Program to Swap Two Numbers using Bitwise OR Operator

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

/* 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 : a = %d  b = %d\n", a, b);
 
  a = a^b;
  b = a^b; 
  a = a^b;
 
  printf("\nAfter  : a = %d  b = %d\n", a, b);
 
  return 0;
}
C Program to Swap Two Numbers using Bitwise Operator

Within this program, 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

The Final Values of the two numbers: a = 4 and b = 8.