Program to swap two numbers with using third variable in C

Source Code

// 8.i. Program to swap two numbers with using third variable
// Arpan Das - CST 2nd Year (VOCLET)

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

void main () {
	int a, b, c;
	//clrscr();
	//system("cls");
	
	printf("\t\t..:: swap two numbers with using third variable ::..\n\n");
	printf("Please enter values for a and b respectively: ");
	scanf("%d%d", &a, &b);
	printf("\nNow a = %d and b = %d\nPress any key to swap...", a, b);
	getch();
	
	c = a;
	a = b;
	b = c;
	
	printf("\n\nSwapped...\n\nNow a = %d and b = %d", a, b);
	
	//getch();
}

Output

		..:: swap two numbers with using third variable ::..

Please enter values for a and b respectively: 3 4

Now a = 3 and b = 4
Press any key to swap...

Swapped...

Now a = 4 and b = 3

Leave a Reply

Note: Your email address will not be published. Required fields are marked *