Swapping of two numbers with third variable in c


There are many methods for swapping two numbers but this is the simplest method for swap the number with the third variable. Beginners can understand this tutorial very easily. We are creating simple Swapping of two numbers with a third variable.

swapping of two numbers with using third variable in c


#include<stdio.h> int main() { int x,y,z; printf("\n Enter first number for x :"); scanf("%d",&x); printf("\n Enter second number for y :\n"); scanf("%d",&y); z=x; x=y; y=z; printf("\n After Swapping the value of x is :%d", x); printf("\n After Swapping the value of y is :%d", y); return 0; }

Output:-


Enter first number for x :22 Enter second number for y :33 After Swapping the value of x is :33 After Swapping the value of y is :22

Explanation of Program :


In this program, three variables are created here x,y and z. The value initialized into only two variables x & y.
We can understand by given below pattern.
x=10 and target is x=20
y=20 y=10
z = x    x value copy into z is 10
x = y    y value copy into x is 20
y = z    z value copy into y is 10