There are two methods to swap two number.

- Using the third variable
- Without using the third variable

## Swapping of number using third variable

In this method we take one other variable and put one variable value to in it. like this

**Algorithm**

Assumption:

we will declare three variable like a, b, c

- put one variable value to the third variable

c=a - a variable value is given to c, now a can hold other variable value

a=b - we substitute b variable value to a mean a value changed with b value, and now b is ready to hold a third variable value which is already containing the value of the first variable
- b=c

if we put all these three lines together, it will look like as

c=a

a=b

b=c

ex:

let declare a=100, b=200

c=a means c=100

a=b means a=200

b=c means b=100

Note: as you can see here we had a=100, b=200 now these values changed as a=200, b=100

## Without using third variable

In this method we can not use of the third variable.

**Algorithm**

assumption

we will declare only two variable a, b

- first we add both variable value like a+b and will substitute on first variable that is a

a=a+b - then we will subtract a by b and will substitute on second variable that is b

b=a-b - again we will subtract a by b and will substitute on first variable that is a

a=a-b

if we put all these three lines together it will look like this

a=a+b

b=a-b

a=a-b

Ex:

let declare a=90 , b=30

a=a+b means a= 90+30, a=120

b=a-b means b=120-30, b=90

a=a-b means a= 120-90, a=30

as you can see, we had a=90, b=30 now values changed as a=30, b=90

**So what will be c program**

```
// swapping of two number using third variable
#include<stdio.h>
#include<conio.h>
int main(){
int a,b,c;
a=100;
b=200;
printf("Before swapping a=%d, b=%d \n",a,b);
c=a;
a=b;
b=c;
printf("After swapping a=%d, b=%d \n",a,b);
return 0;
}
```

```
// swapping of two number without using third variable
#include<stdio.h>
#include<conio.h>
int main(){
int a,b;
a=100;
b=200;
printf("Before swapping a=%d, b=%d \n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping a=%d, b=%d \n",a,b);
return 0;
}
```