

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swap two variables in one line in C/C+
Here is an example of swapping in C language,
Example
#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
After Swapping : 828 After Swapping again : 288
In the above program, there are two variables a and b and initialized with the values 28 and 8 respectively. There are so many methods to swap two numbers in one line and we displayed two methods here.
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);
- Related Questions & Answers
- Swap two variables in one line using C#
- Swap two variables in one line in Java
- Swap two variables in one line in using Java
- Swap two variables in one line in using Python?
- Swap two variables in one line in C/C++, Python, PHP and Java
- How to Swap Two Variables using Python?
- Swap two numbers in C#
- How to swap two String variables without third variable.
- Largest number with one swap allowed in C++
- C++ Program to Swap Two Numbers
- C program to swap two strings
- How to swap variables with destructuring in JavaScript?
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- Next higher number using atmost one swap operation in C++
- Previous Permutation With One Swap in Python
Advertisements