C Program to Swap Two Numbers

In this tutorial, i am going to show you how to swap two numbers in c program with the help of third variable, pointers, functions, call by reference and bitwise or operator.

All Programs to Swap Two Numbers in C

  • C Program to Swap Two Numbers using Third Variable
  • C Program to Swap Two Numbers Using Pointers
  • C Program to Swap Two Numbers Using Functions
  • C Program to Swap Two Numbers Using Call By Reference
  • C Program to Swap Two Numbers Using Bitwise OR Operator

C Program to Swap Two Numbers using Third Variable

#include <stdio.h>
int main()
{
  int a, b, Temp;
 
  printf("\nPlease Enter the value of a and b :- ");
  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;
}

The result of the above c program; as follows:

Please Enter the value of a and b :- 20 40
Before Swap: a = 20 and b = 40

After Swapping: a = 40 and b = 20

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 :- ");
  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;
}

The result of the above c program; as follows:

Please Enter the value of a and b :- 50 60
Before Swap: a = 50  b = 60

After Swapping: a = 60  b = 50

After Swapping: i = 60  j = 50

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 :- ");
  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);
}

The result of the above c program; as follows:

Please Enter the value of A and B :- 50 70
Before Swap: A = 50 and A = 70

After Swapping: A = 70 and B = 50

C Program to Swap Two Numbers Using Call By Reference

#include <stdio.h>
int main()
{
  int a, b;
 
  printf("\nPlease Enter the value of a and b :- ");
  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;
}

The result of the above c program; as follows:

Please Enter the value of a and b :- 66 89
Before Swap: a = 66  b = 89

After Swapping: a = 89  b = 66

C Program to Swap Two Numbers Using Bitwise OR Operator

/* Swap Two Numbers Using Bitwise OR Operator */
#include <stdio.h>
int main()
{
  int a, b;
 
  printf("\nPlease Enter the value of a and b :- ");
  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;
}

The result of the above c program; as follows:

Please Enter the value of a and b :- 56 85
Before Swap: a = 56  b = 85

After Swapping: a = 85  b = 56

More C Programming Tutorials

Leave a Comment