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

C# Program to swap two numbers using temporary variable.

C# program to swap two numbers using temporary variable

We have already discussed about the C# program to swap two numbers without using temporary variable. In this example, we will learn how to write C# program to swap two numbers using a temporary variable. Basically, swapping refers to exchanging the values eachother. It is really simple to swap two numbers using temporary variable. Now, let’s see how we will 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 using temporary variable.

Let us consider, num1=10 and num2=30

temp=num1=10
num1=num2=30
num2=temp=10

Hence, the numbers are swapped.

Algorithm to swap two numbers using the third/temporary variable.

We will use variables “num1” and “num2” to store values of two numbers. We will need an extra variable to swap values. Algorithm is given as follows:

Step 1: Start.
Step 2: Take two inputs in variable num1 and num2.
Step 3: Assign the value of num1 to temporary (or third) variable (say "temp").
Step 4: Then, assign the value of num2 to num1.
Step 5: Assign the value of third variable to num1.
Step 6: End.

Program to swap two numbers using a third/temporary variable.

using System;
namespace swap
{
	class Program
	{
		static void Main(String[] args)
		{
			int num1, num2, temp;
                       Console.WriteLine("Enter value of num1:");
                       num1=int.Parse(Console.ReadLine());                   //number is stored in num1
                       Console.WriteLine("Enter value of num2:");
                       num2= int.Parse(Console.ReadLine());                  //number is stored in num2

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

			//swapping
			temp=num1;                         //num1 is assigned to temp
			num1=num2;                        //num2 is assigned to num1
			num2=temp;                         //temp is assigned to 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
Swapping two numbers using temporary variable in C# Programming Language

Also Read: C Program to Swap two numbers without Using a Temporary Variable

Explanation:

  • Since, we are using the third variable to swap numbers, we have to declare three variables.
int num1, num2, temp;

We will take input in those variables; num1 and num2. The variable temp is used to store value temporarily.
In the above program, 10 & 30 are given as input for num1 & num2 respectively.

num1=10
num2=30
  • After that, we will start the steps to swap those numbers.
temp=num1;

Here, we have stored the value of num1 to temp (third variable).

temp=10
  • Then, assign value of num2 to num1.
num1=num2;

Here, we have replaced the value of num1 by the value of num2.

num1=30
  • Then, assign the value of temp to num2.
num2=temp;

Here, the value of num2 is replaced by the value of third variable temp (that has stored the initial value of num1).

num2=10
  • Finally, we’ve,
num1=30
num2=10

Also Read:
C# Hello World
C Sharp for Beginners
C# Program to print out Fibonacci Series

Leave a Reply

Your email address will not be published. Required fields are marked *