Given two numbers, write a C program to swap the given numbers.
Input : x = 10, y = 20; Output : x = 20, y = 10 Input : x = 200, y = 100 Output : x = 100, y = 200

The idea is simple
- Assign x to a temp variable : temp = x
- Assign y to x : x = y
- Assign temp to y : y = temp
Let us understand with an example.
x = 100, y = 200
After line 1: temp = x
temp = 100After line 2: x = y
x = 200After line 3 : y = temp
y = 100
// C program to swap two variables #include <stdio.h> int main() { int x, y; printf("Enter Value of x "); scanf("%d", &x;); printf("\nEnter Value of y "); scanf("%d", &y;); int temp = x; x = y; y = temp; printf("\nAfter Swapping: x = %d, y = %d", x, y); return 0; } |
chevron_right
filter_none
Output:
Enter Value of x 12 Enter Value of y 14 After Swapping: x = 14, y = 12
How to write a function to swap?
Since we want the local variables of main to modified by swap function, we must them using pointers in C.
// C program to swap two variables using a // user defined swap() #include <stdio.h> // This function swaps values pointed by xp and yp void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } int main() { int x, y; printf("Enter Value of x "); scanf("%d", &x;); printf("\nEnter Value of y "); scanf("%d", &y;); swap(&x;, &y;); printf("\nAfter Swapping: x = %d, y = %d", x, y); return 0; } |
chevron_right
filter_none
Output:
Enter Value of x 12 Enter Value of y 14 After Swapping: x = 14, y = 12
How to do in C++?
In C++, we can use references also.
// C++ program to swap two variables using a // user defined swap() #include <stdio.h> // This function swaps values referred by // x and y, void swap(int &x;, int &y;) { int temp = x; x = y; y = temp; } int main() { int x, y; printf("Enter Value of x "); scanf("%d", &x;); printf("\nEnter Value of y "); scanf("%d", &y;); swap(x, y); printf("\nAfter Swapping: x = %d, y = %d", x, y); return 0; } |
chevron_right
filter_none
Output:
Enter Value of x 12 Enter Value of y 14 After Swapping: x = 14, y = 12
Is there a library function?
We can use C++ library swap function also.
// C++ program to swap two variables using a // user defined swap() #include <bits/stdc++.h> using namespace std; int main() { int x, y; printf("Enter Value of x "); scanf("%d", &x;); printf("\nEnter Value of y "); scanf("%d", &y;); swap(x, y); printf("\nAfter Swapping: x = %d, y = %d", x, y); return 0; } |
chevron_right
filter_none
Output:
Enter Value of x 12 Enter Value of y 14 After Swapping: x = 14, y = 12
How to swap without using a temporary variable?
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Python Program to swap two numbers without using third variable
- TCS Coding Practice Question | Swap two Numbers
- Maximise the sum of two Numbers using at most one swap between them
- How to swap two numbers without using a temporary variable?
- C++ Program to swap two members using Friend Function
- Python Program to Swap Two Variables
- Java Program to Swap two Variables
- Swap two nibbles in a byte
- Minimize cost to Swap two given Arrays
- Swap the elements between any two given quadrants of a Matrix
- Swap three numbers in cycle
- C program to swap adjacent characters of a String
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Form the largest number using at most one swap operation
- Form the smallest number using at most one swap operation
- Next higher number using atmost one swap operation
- Largest smaller number possible using only one swap operation
- Swap major and minor diagonals of a square matrix
- valarray swap() function in c++
- Swap characters in a String

