Programming

ShareConsider we have two variables x and y containing some number. We are asked to swap the values without using a third variable. To solve this problem we follow the given steps (algorithm)

```
x = x + y
y = x - y
x = x - y
```

```
Let x = 10 and y = 20
So,
x = x + y
= 10 + 20
= 30
y = x - y
= 30 - 20
= 10
x = x - y
= 30 - 10
= 20
```

```
Let x = 10 and y = -20
So,
x = x + y
= 10 + (-20)
= -10
y = x - y
= (-10) - (-20)
= (-10) + 20
= 10
x = x - y
= (-10) - 10
= -20
```

```
Let x = -10 and y = -20
So,
x = x + y
= (-10) + (-20)
= -30
y = x - y
= (-30) - (-20)
= (-30) + 20
= -10
x = x - y
= (-30) - (-10)
= (-30) + 10
= -20
```

```
#include <stdio.h>
int main(){
//variables
int x, y;
//input
printf("Enter value of x and y: ");
scanf("%d%d", &x, &y);
//swap
x = x + y;
y = x - y;
x = x - y;
//output
printf("x = %d y = %d\n", x, y);
return 0;
}
```

- How to install Apache, MySQL, PHP on macOS Mojave 10.14 How to Mac
- Exercise 1 Flowchart
- How to install Apache, MySQL, PHP on macOS Catalina 10.15 How to Mac
- How to install PostgreSQL on Mac using Homebrew How to Mac
- How to change default shell to bash on macOS Catalina How to Mac
- How to backup and restore MySQL or MariaDB database using mysqldump Reference Database
- HTML Getting Started HTML
- ChartJS | How to create Doughnut Chart using data from MySQL (MariaDB) table and PHP ChartJS
- MongoDB - Delete Documents MongoDB
- MongoDB - Update Documents MongoDB