Home >>c programs >C Program to swap two numbers
Swapping two number in C programming language defines the exchange values of two variables. Let’s suppose you have two variable X & Y. Value of X is 15 & value of Y is 25. So, after swapping the value, X will become 25 & Y will become 15.
You can swap two numbers without using third variable
Let's take an example
#include<stdio.h> int main() { int x=10, y=20; printf("Before swaped number x=%d y=%d",x,y); x=x+y;//a=30 (10+20) y=x-y;//y=10 (30-20) x=x-y;//x=20 (30-10) printf("\nAfter swaped Number x=%d y=%d",x,y); return 0; }
#include<stdio.h> int main() { int x=10, y=20,z=0; printf("Before swaped number x=%d y=%d",x,y); z=x; x=y; y=z; printf("\nAfter swaped Number x=%d y=%d",x,y); return 0; }