In this program, we will explain how to swap two numbers without using any third variable using c programming language.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> int main() { int x = 10, y = 20; x = x + y; // x now becomes 30 y = x - y; // y becomes 10 x = x - y; // x becomes 20 printf("After Swapping: x = %d, y = %d", x, y); return 0; } |
After Swapping: x = 20, y = 10
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c programs, c programs with solutions, list of important c programs, Number Swapping, Number Swapping program, Number Swapping using the temporary variable. c plus plus tutorial