Skip to content
Home » Programming » C Examples » Swap two numbers Using a Temporary Variable in C

Swap two numbers Using a Temporary Variable in C

Swap two numbers Using a Temporary Variable in C

In this example, we will learn how to swap two numbers using a temporary variable in C.

Main idea behind the solution

Here is the main idea to swap two numbers using third variable.

Lets say num1 = 20 and num2 = 54.  We will use the extra “temp” variable to solve this problem.

temp = num1 = 20
num1 = num2 = 54
num2 = temp = 20

Hence, the numbers are swapped.

Algorithm to swap two numbers using a temporary variable

At first, we will need a temporary variable (let’s take “temp”) to store the intermediate value. We will use variable num1 and num2 to store values. You can use any variable names. Here is the algorithm for swapping two variables using third variable.

Step 1: Start.
Step 2: Take two inputs in variable num1 and num2.
Step 3: Assign value of num1 to temp.
Step 4: Then, assign the value of num2 to num1.
Step 5: Finally, assign the value of temp to num2. Note that temp variable stores the initial value of the num1 variable. 
Step 5: End.

In short,

Start,
    temp <- num1 /* now temp holds the value of num1 */
    num1 <- num2 /* now num1 holds the value of num2 */
    num2 <- temp /* finally num2 holds the value of num1 which is stored in temp */
End.

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

Program

#include<stdio.h>

int main() {
      int num1, num2, temp;

      /* Input */
      printf("Enter first number: ");
      scanf("%d", &num1);
      printf("Enter second number: ");
      scanf("%d", &num2);
	
      /* Swapping */

      // Value of num1 i.e first variable is assigned to temp
      temp = num1;

      // Value of num2 i.e second variable is assigned to first
      num1 = num2;

      // Value of temp  is assigned to num2 variable
      num2 = temp;

      /* Output */
      printf("\nAfter swapping: ");
      printf("\nValue of First Number = %d", num1);
      printf("\nValue of Second Number = %d", num2);
      return 0;
}

Output

Enter first number: 20
Enter second number: 54                                              
                                                                     
After swapping:                                                      
Value of First Number = 54                                          
Value of Second Number = 20

Explanation

  • At first, we assigned the value of the first number i.e. num1 to the temp variable. That means, now temp holds the value of the first number and we can use it to store the value of another variable since its value is safe.
temp = num1 // num1 = 20
So,
temp = 20
  • After that, we assigned the num2 to num1. Now the second number is stored in the first variable.
num1 = num2 // num2 = 54
So,
num1 = 54
  • At last, we assigned the value of the temp variable to the num2. Finally, the value of the first variable is stored in the second variable.
num2 = temp // temp = 20
So,
num2 = 20

Finally,

num1 = 54
num2 = 20

Hence, value is swapped using a temporary variable.

More Examples

Leave a Reply

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