onlinecode

c programming – Swap two variable without using 3rd variable

c programming – Swap two variable without using 3rd variable

In this post we will show you how to Swap two variable without using 3rd variable in c programming. flowing code will help you to perform program.
For Swap two variable without using 3rd variable we use this method ::
a=a+b;
b=a-b;
a=a-b;

// include header file 
# include <stdio.h>
# include <conio.h>
// main function
void main()
{
	int a,b;
	clrscr();
	
	// insert data
	printf("Enter A: ");
	scanf("%d",&a);
	printf("Enter B: ");
	scanf("%d",&b);

	// print BeforeSwap
	printf("\nBefore Swap:");
	printf("\nValue of A is: %d",a);
	printf("\nValue of B is: %d",b);
	
	// Swap data
	a=a+b; 
	b=a-b; 
	a=a-b;

	// print Swap result
	printf("\n\nAfter Swap:");
	printf("\nValue of A is: %d",a);
	printf("\nValue of B is: %d",b);
	getch();
}
Exit mobile version