English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This is an example of swapping using C language
#include <stdio.h> int main() { int a = 28, b = 8; a += b -= a = b - a; // method 1 printf("After Swapping : %d\t%d", a, b); (a ^= b), (b ^= a), (a ^= b); // method 2 printf("\nAfter Swapping again : %d\t%d", a, b); return 0; }
Output Result
After Swapping : 828 After Swapping again : 288
In the above program, there are two variables a and b, which are assigned values separately28and8There are too many ways to swap two numbers in one line, and here we show two methods.
a += b -= a = b - a; // method 1 printf("After Swapping : %d\t%d", a, b); (a ^= b), (b ^= a), (a ^= b); // method 2 printf("\nAfter Swapping again : %d\t%d", a, b);