Swap of two numbers in C without using third variable.

Step 1:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter any two no “);
scanf(“%d%d”,&a,&b);
{
a=a+b;
b=a-b;
a=a-b;
}
printf(“\n no are : %d%d”,a,b);
getch();
}

1

Step 2: Save it by pressing F2 and name it SWAP.CPP and clock OK.

2

Step 3: Now compile it by click on compile then click OK or by pressing ALT+F9 to find errors and correct them.

3

Step 4: After compiling press ALT+R to run the program and see the output.

//Output :

Suppose we enter 10 and 11 ,then the result is :

11 and 10.

4

Tags: