C Program to Swap Two Numbers using Temporary Variable


Program


#include<stdio.h>
#include<conio.h>

int main()
{
	 int a, b, temp;
	 clrscr();
	 printf("Enter value of a: ");
	 scanf("%d", &a);
	 printf("Enter value of b: ");
	 scanf("%d", &b);
	 printf("Before swapping: a = %d and b = %d\n", a, b);
	 temp = a;
	 a = b;
	 b = temp;
	 printf("After swapping: a = %d and b = %d", a, b);
	 getch();
	 return(0);
}

Output of the above program :

Enter value of a: 23 ↲
Enter value of b: 17 ↲
Before swapping: a = 23 and b = 17
After swapping: a = 17 and b = 23

Note: ↲ indicates ENTER is pressed.