C Program to swap two variables using addition and subtraction without using temporary variable
Swapping two variables using addition and subtraction in such a way that values get interchanged by following method $$a = a + b$$ $$b = a - b$$ $$a = a - b$$ The values 'a' and 'b' are printed after swapping.
Program
#include<stdio.h>
void main()
{
int a, b;
printf("Enter two variables\n");
printf("a:\t");
scanf("%d", &a);
printf("b:\t");
scanf("%d", &b);
printf("Values of a and b before swapping\n");
printf("a:\t%d\n", a);
printf("b:\t%d\n", b);
a = a + b;
b = a - b;
a = a - b;
printf("Values of a and b after swapping\n");
printf("a:\t%d\n", a);
printf("b:\t%d\n", b);
}
Output
Enter two variables
a: 10
b: 55
Values of a and b before swapping
a: 10
b: 55
Values of a and b after swapping
a: 55
b: 10