C Program to swap two variables using temporary variable
Swapping two variables using a third variable 'temp' to hold a value and shift it to another variable. Swapping is done as follows: $$temp = a$$ $$a = b$$ $$b = temp$$ The values 'a' and 'b' are printed after swapping.
Program
#include<stdio.h>
void main()
{
int a, b, temp;
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);
temp = a;
a = b;
b = temp;
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: 71
Values of a and b before swapping
a: 10
b: 71
Values of a and b after swapping
a: 71
b: 10