Home » Programming » C# Program to swap two numbers without using temporary variable.

# C# Program to swap two numbers without using temporary variable.

We have already discussed about the C# program to swap two numbers using temporary variable. In this example, we will learn how to write C# program to swap two numbers without using a temporary variable. Basically, swapping refers to exchanging the values eachother. It is really simple to swap two numbers even without using temporary variable.
Now, let’s see how we can swap two numbers in C#:

## Main idea behind the solution

First of all, let’s go through the main idea behind the swapping two numbers without using temporary variable.

```Let us consider, num1=10 and num2=30

num1 = num1 + num2 = 10 + 30 = 40
num2 = num1 - num2 = 40 - 30 = 10
num1 = num1 - num2 = 40 - 10 = 30```

Hence, the numbers are swapped.

## Algorithm to swap two numbers without using the temp variable

We will be using variables “num1” and “num2” to store values of two numbers. We don’t need any extra variable to swap values. Algorithm is given as follows:

```Step 1: Start.
Step 2: Take two inputs & store in variable num1 and num2.
Step 3: Add num1 and num2 and assign it to num1.
Step 4: Then, subtract num2 from num1. Now, num2 holds the value of num1.
Step 5: Again, subtract num2 from num1 & assign it to num1. Hence, num1 holds the value of num2.
Step 6: End.```

In short,

```Start,
num1 <- num1 + num2 // num1 holds the total sum of both numbers
num2 <- num1 - num2 // then, num2 holds the value of num1
num1 <- num1 - num2 // Finally num1 holds the value of num2
End.```

## Program to swap two numbers without using a temporary variable

```using System;
namespace swap
{
class Program
{
static void Main(String[] args)
{
int num1, num2;
Console.WriteLine("Enter the value of num1:");
Console.WriteLine("Enter the value of num2:");

//Before Swapping
Console.WriteLine("Before Swapping");
Console.WriteLine("num1="+num1);
Console.WriteLine("num2="+num2);

//swapping
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;

//After Swapping
Console.WriteLine("Values after swapping are:");
Console.WriteLine("num1="+num1);
Console.WriteLine("num2="+num2);
}
}
}```

### Output

```Enter value of num1:
10
Enter value of num2:
30
Before Swapping
num1=10
num2=30
Values after swapping are:
num1=30
num2=10```

### Explanation

• Since we are not using any third variable to swap numbers we only have to declare two variables.
`int num1, num2;`

We will take input in those variables; num1 and num2.

• After that we will start the steps to swap those numbers.
```num1 = num1 + num2 // 10 + 30 = 40 since num1 = 10 and num2 = 30
So,
num1 = 40```

At first, we took the sum of both num1 and num2 and assigned it to the num1 variable.

• Then, we subtracted the value of num2 from the num1 (here, num1 holds the sum of the num1 and num2).
```num2 = num1 - num2; // 40 - 30 = 10 since num1 = 40 and num2 = 30
So,
num2 = 10 // which is the initial value of num1 i.e. 10```

When we subtract we are left with the value of num1 only now which is then assigned to the num2 variable.

• Similarly, subtract num2 from num1 and assign it to num1.
```num1 = num1 - num2; // 40 - 10 = 30 since num1 = 40 and num2 = 10
So,
num1 = 30 // which is the initial value of num2 i.e. 30```
• Finally,
```num1 = 30
num2 = 10```

Hence, value is swapped without using a temporary variable.