#include<stdio.h>
#include<conio.h>
int main()
{
int a=7,b=4;
printf("\n A=%d B=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nNow A=%d B=%d",a,b);
getch();
}
/*In the above program,no third variable is used as a mediator for swapping the values.*/
This program swaps the values of variables a and b without using a third variable.
First, the values of a and b are initialized to 7 and 4, respectively. Then, the values of a and b are swapped using the following steps:
ais assigned the sum ofaandbi.e.a = a + b.bis assigned the difference ofaand the original value ofbi.e.b = a - b. At this point,bholds the original value ofa.ais assigned the difference of the original value ofaand the new value ofbi.e.a = a - b. At this point,aholds the original value ofb.
Finally, the program prints the values of a and b, which are swapped, by using the printf() function.
Thanks
Write a program to swap the value of two variables without the use third variable.