
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 Variable Values Without Using Third Variable in C/C++
The following is an example of swapping two variables.
Example
#include <stdio.h> int main() { int a,b; printf("Enter the value of a : "); scanf("%d", &a); printf("\nEnter the value of b : "); scanf("%d", &b); a += b -= a = b - a; printf("\nAfter Swapping : %d\t%d", a, b); return 0; }
Output
Enter the value of a : 23 Enter the value of b : 43 After Swapping : 4323
In the above program, two variables a and b are declared and initialized dynamically at run time.
int a,b; printf("Enter the value of a : "); scanf("%d", &a); printf("\nEnter the value of b : "); scanf("%d", &b);
Numbers are swapped without using any third variable.
a += b -= a = b - a;
Advertisements