- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swapping two variable value 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;
- Related Articles
- Swap two Strings without using third user defined variable in Java
- Swap two Strings without using temp variable in C#
- How to swap two numbers without using the third or a temporary variable using C Programming?
- How to swap two String variables without third variable.
- How can I swap two strings without using a third variable in Java?
- Write a Golang program to swap two numbers without using a third variable
- How to swap two numbers without using a temp variable in C#
- How to swap two arrays without using temporary variable in C language?
- How to find the size of a variable without using sizeof in C#?
- Assign other value to a variable from two possible values in C++
- Swap Numbers without using temporary variable in Swift Program?
- Variable initialization in C++
- Update MongoDB variable value with variable itself?
- Variable Length Argument in C
- Variable Arguments (Varargs) in C#

Advertisements