C Program to Swap Two Numbers

C program to swap two numbers; Through this tutorial, we will learn how to swap two numbers in c program using third variable, pointers, functions, call by reference and bitwise or operator.

Programs to Swap Two Numbers in C

Let’s use the following program to swap two numbers in c using third variable, pointer, function, OR Operator, and call by reference:

  • 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 output 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 output 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 output 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 output 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 output 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

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *