In this blog post, we learn how to write a C program to swap two numbers using a temporary variable or third variable and also without using a temporary or third variable?. We will write the C program to swap two numbers using the Arithmetic Operators, Bitwise Operators. We will also create a function two swap two numbers using call by reference.
What is swapping mean?
Let’s understand the below example to understand the swapping. Suppose you have two numbers which have values 10 and 20. After the swapping there value will be interchanged.
Input : x = 10, y = 20; || \/ Output : x = 20, y = 10
C Program to swap two numbers using a third variable or temp variable
You can easily swap two numbers using the third variable. see the below concept.
Step1: temp = var1; Step2: var1 = var2; Step3: var2 = temp;
#include <stdio.h> int main() { int a, b,tmp; printf("Enter Value of a = "); scanf("%d", &a); printf("Enter Value of b = "); scanf("%d", &b); temp = a; a = b; b = temp; printf("\nAfter Swapping: a = %d, b = %d", a, b); return 0; }
Output:
Enter Value of a = 10
Enter Value of b = 20
After Swapping: a = 20, b = 10
C Program to swap two numbers without using a third variable or temp variable
You can write a C program to swap two numbers without using the third variable mainly two ways. The first one is by using the arithmetic operator and the second one is using the ex-or operator.
Method 1. Swapping two numbers using arithmetic operator:
In this method, we will calculate the sum of two given numbers and assign one of them. The numbers can then be swapped using the subtraction from the sum. See the below C program,
#include <stdio.h> int main() { int a = 10, b = 20; a = a + b; // a now becomes 30 b = a - b; // b becomes 10 a = a - b; // a becomes 20 printf("After Swapping: a = %d, b = %d", a, b); return 0; }
Output:
Enter Value of a = 10
Enter Value of b = 20
After Swapping: a = 20, b = 10
Method 2. Swapping two numbers using ex-or operator:
The bitwise XOR operator can be used to swap two variables. Here the concept is that EX-OR of two same numbers is zero. The XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of a and b differ.
#include <stdio.h> int main() { int a, b ; printf("Enter Value of a = "); scanf("%d", &a); printf("Enter Value of b = "); scanf("%d", &b); a = a ^ b; // a now becomes 30 b = a ^ b; // b becomes 10 a = a ^ b; // a becomes 20 printf("After Swapping: a = %d, b = %d", a, b); return 0; }
Output:
Enter Value of a = 10
Enter Value of b = 20
After Swapping: a = 20, b = 10
C Program to swap two numbers using the function
Using call by reference we can create a function to swap two numbers. See the below example code,
#include <stdio.h> void SwapTwoNumber(int *a, int *b) { // Check if the two addresses are same if(*a == *b) return; *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } int main() { int x = 10; int y = 20; SwapTwoNumber(&x, &y); printf("x = %d and y = %d",x,y); return 0; }
Output:
x = 20 and y = 10
Recommended Post for you:
- C program to add two integer numbers
- C program to find the factorial of a number
- C Program to swap two nibbles in a byte
- C Program to find first and last digit of a given number
- C program to find even and odd numbers
- C Program to find the sum of natural numbers up to n terms
- C Program to find the sum of even natural numbers from 1 to n
- C Program to find the sum of odd natural numbers from 1 to n
- C Program to find if given number is sum of first n natural numbers
- C program to find sum of first and last digit of a Number
- C program to find the sum of digits of a number
- C program to check leap year
- C program to check valid date (date is valid or not)
- C program to Check expiry date
- C program to find reverse of a number using recursion
- C program to reverse digits of an integer with overflow handled