C Program to Swap Two Numbers

In the C programming language, swapping two numbers indicates swapping the values of two variables. Consider that you have the variables var1 and var2. Var1 has a value of 20 and Var2 has a value of 40. Therefore, after swapping, the values of var1 and var2 will be 40 and 20, respectively. You’ll learn how to swap two variables in C in this blog.

Algorithm to Swap Two Number:

  1. START
  2. ENTER x, y
  3. PRINT x, y
  4. x = x + y
  5. y= x – y
  6. x =x – y
  7. PRINT x, y
  8. END

Different Methods to Swap Two Numbers:

  1. Swapping Two Numbers Using Third Variable
  2. Swapping Two Numbers Using Without Using Third Variable
  3. Swapping Function in C
  4. Swap two numbers using pointers in C
  5. Swap Two Numbers Using Bitwise XOR

We will look at each one of them one by one.

Method 1: Swapping Two Numbers Using Third Variable Logic

The concept behind employing a third variable to swap two integers is straightforward. Put the first variable’s value into a temporary variable. Put the second variable’s value into the first variable. Finally, add the temp variable’s value to the second variable. To hold the value of the first variable in our program we are utilizing a temporary variable.

  • Assign var1 value to a temp variable: temp = var1
  • Assign var2 value to var1: var1 = var2
  • Assign temp value to var2: var2 = temp

Code Implementation:

#include <stdio.h> 
int main()
{
	int var1, var2, temp; 
	printf("Enter two integers \n");
	scanf("%d%d", &var1, &var2);
	printf("Before Swappingn First variable = %d\nSecond variable = %d \n", var1, var2);
	temp = var1;
	var1 = var2;
	var2 = temp;
	printf("After Swappingn First variable = %d\nSecond variable = %d\n", var1, var2);
	return 0;
}

Method 2: Swapping Two Numbers Using Without Using Third Variable

We are not storing the value in any temporary variables when we swap two variables in this variation. The sum of the two variables is kept in the first variable. The value of the second variable is then subtracted from the sum in the following step to extract the value of the first variable, which is then stored in the second variable. The first variable will now receive the original value that was stored in the second variable.

Code Implementation:

#include <stdio.h>
int main()
{
	int var1, var2, temp;
	printf("Enter two integers\n");
	scanf("%d%d", &var1, &var2);
	printf("Before Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2);
	var1 = var1 + var2;
	var2 = var1 - var2;
	var1 = var1 - var2;
	printf("After Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2);
	return 0;
}

Method 3: Swapping Function in C

Any swapping variant may be implemented as a swapping function in C, which can be called whenever you wish to swap two variables. We need to use pointers because we want the swap function to modify the main’s local variables.

Code Implementation:

void swap(int *num1, int *num2) 
{ 
	int temp = *num1; 
	*num1 = *num2; 
	*num2 = temp; 
}   
int main() 
{ 
	int var1, var2; 
	printf("Enter Value of var1 "); 
	scanf("%d", &var1); 
	printf("\nEnter Value of var2 "); 
	scanf("%d", &var2); 
	swap(&var1, &var2); 
	printf("\nAfter Swapping: var1 = %d, var2 = %d", var1, var2); 
	return 0; 
}

Method 4: Swap two numbers using pointers in C

You can also swap two variables using pointers, wherein you pass the address of the variables to two different variables. Then, swap their values.

Code Implementation:

#include <stdio.h> 
int main()
{
	int var1, var2, *num1, *num2, temp;
	printf("Enter the value of var1 and var2\n");
	scanf("%d%d", &var1, &var2);
	printf("Before Swapping\nvar1 = %d\nvar2 = %d\n", var1, var2);
	num1 = &var1;
	num2 = &var2;
	temp = *num2;
	*num2   = *num1;
	*num1   = temp;
	printf("After Swapping\nvar1 = %d\nvar2 = %d\n", var1, var2);
	return 0;
}

Method 5: Swap Two Numbers Using Bitwise XOR

The XOR operator functions similarly to swapping without the need of temporary variables. We extract both variables’ XOR calculations. Next, we exchange specific values that we extracted.
Assume that var1 is 20 and var2 is 40. 20 is worth 10100 bits in binary, while 40 is worth 101000.

Code Implementation:

#include <stdio.h>
int main()
{
	int var1, var2, temp; 
	printf("Enter two integers\n");
	scanf("%d%d", &var1, &var2);
	printf("Before Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2); 
	var1 = var1 ^ var2;
	var2 = var1 ^ var2;
	var1 = var1 ^ var2;
	printf("After Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2);
	return 0;
}

You would now have a thorough understanding of all the many ways to swap two integers in C after running the above program. I hope you find my site to be useful and helpful.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C

Leave a Reply

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