In this program, you will learn how to swap two numbers. It is also know as swapping or exchange of two numbers. Here we will take another variable called temp for temporary storing the number. This is swapping using third variable.
#include<stdio.h>
void main()
{
int n1, n2, temp;
printf("Please enter the values of a and b: ");
scanf("%d%d", &n1, &n2);
printf("\nBefore swapping\n a = %d and b = %d", n1, n2);
temp = n1;
n1 = n2;
n2 = temp;
printf("\n\nAfter swapping\n a = %d and b = %d", n1, n2);
getch();
}
Please enter the values of a and b: 74 52
Before swapping
a = 74 and b = 52
After swapping
a = 52 and b = 74