Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program to Swap Two Numbers

Last Updated on May 16, 2023 by Prepbytes

Swapping two integers in a computer language means switching the values of two variables. Think about the variables var1 and var2 you have. Var1 is valued at 20, whereas Var2 is valued at 40. As a result, after swapping, var1 and var2 will have values of 40 and 20, respectively. This blog post will teach you how to swap two variables in C.

Algorithm to Swap Two Numbers in C:

  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 in C:

  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 in C Using Third Variable Logic

To swap two numbers, the idea behind using a third variable is simple. Put the value of a temporary variable into the first variable. Put the value of the second variable into the first one. Add the value of the temp variable to the second variable at the end. We are using a temporary variable in our program to store the value of the first 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 of C program to swap two numbers:

#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 in C Using Without Using Third Variable

When we swap two variables in this variant, we are not saving the value in any temporary variables. The first variable is preserved as the two variables’ total. The value of the first variable is then extracted and saved in the second variable by subtracting its value from the sum in the following step. Now the original value kept in the second variable will be transferred to the first variable.

Code Implementation of C program to swap two numbers:

#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

In C, you may implement every swapping version as a swapping function that you can use to swap two variables anytime you need to. Because we want the swap function to change the local variables in the main, pointers are required.

Code Implementation of C program to swap two numbers:

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

Using pointers, you may swap two variables by passing their addresses to two separate variables. Change the settings after that.

Code Implementation of C program to swap two numbers:

#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 in C Using Bitwise XOR

Without the use of temporary variables, the XOR operator performs operations in a manner similar to swapping. We take the XOR results for both variables. Then, we trade particular values that we extracted.

Let var1 be 20 and var2 be 40. In binary, 20 is equivalent to 10100 bits, and 40 to 101000.

Code Implementation of C program to swap two numbers:

#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;
}

Conclusion
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 our site to be useful and helpful.

Frequently asked Questions

Q1. How do you swap two numbers?
Ans. Let’s take an example, there are two numbers 50 and 70. The first number is 50 and the second number is 70 after performing the swapping logic the first number will become 70 and the second number will be 50 respectively.

Q2. Is there a swap command in C?
Ans. No, there is no standard function in C to swap two numbers.

Q3. What is a swap in an array?
Ans. Destructuring is a much better way to swap array items because it just requires one line of code. Simply create a new array with both items in a specific order, then assign that array’s contents to another array with both elements in the opposite order.

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 *