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

C Program to Swap two numbers without Using a Temporary Variable

to swap two numbers without using a temporary variable

In this example, we will learn how to swap two numbers without using a temporary variable in C. It might seem really confusing at first without using a temporary variable but it’s just the basic mathematics operations. Let’s see how we will solve it.

Main idea behind the solution

Here is the main idea to swap two numbers without using a temporary variable.

Lets say num1 = 20 and num2 = 54

num1 = num1 + num2 = 20 + 54 = 74
num2 = num1 - num2 = 74 - 54 = 20
num1 = num1 - num2 = 74 - 20 = 54

Hence, the numbers are swapped.

Algorithm to swap two numbers without using a temporary variable

We will use variables “num1” and “num2” to store values of two numbers. We will not need any 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: 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 and 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.

Also Read: C Program to Find ASCII Value of a Character & Vice Versa

Program to swap two numbers without using a temporary variable

#include<stdio.h>

int main() {   
     int num1, num2;
     printf("Enter first number: ");
     scanf("%d", &num1);
     printf("Enter second number: ");
     scanf("%d", &num2);


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

     printf("\nAfter swapping: ");
     printf("\n Value of First Number = %d", num1);
     printf("\n Value 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

  • 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 // 20 + 54 = 74 since num1 = 20 and num2 = 54
So,
num1 = 74

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; // 74 - 54 = 20 since num1 = 74 and num2 = 54
So,
num2 = 20 // which is the initial value of num1 i.e. 20

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; // 74 - 20 = 54 since num1 = 74 and num2 = 20
So, 
num1 = 54 // which is the initial value of num2 i.e. 54
  • Finally,
num1 = 54
num2 = 20

Hence, value is swapped without using a temporary variable.

Also Read: C program to Check whether a number is positive, negative or zero

Using Multiplication and Division (Using * and /)

The main idea behind using multiplication and division is the same as the using addition and subtraction operators. But we have one drawback using this method, the number will not be swapped if also only one of them is zero.

Lets say num1 = 20 and num2 = 54 

num1 = num1 * num2 =  = 1080
num2 = num1 - num2 = 1080 / 54 = 20
num1 = num1 - num2 = 1080 / 20 = 54

Program

/*
 * This Program will not work even if only one of the numbers  is zero.
*/

#include<stdio.h>

int main() {   
     int num1, num2;
     printf("Enter first number: ");
     scanf("%d", &num1);
     printf("Enter second number: ");
     scanf("%d", &num2);


     /*swapping*/
     num1 = num1 * num2; 
     num2 = num1 / num2;
     num1 = num1 / num2;

     printf("\nAfter swapping: ");
     printf("\n Value of First Number = %d", num1);
     printf("\n Value 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 multiplied both num1 and num2 and assigned it to num1.
num1 = num1 * num2; // 20 * 54 = 1080 since num1 = 20 and num2 = 54
So,
num1 = 1080
  • Then, we divided the num1 by num2 and assigned it to num2. This means that we have filtered out the value of num1 from the multiplied value. So, now num2 contains the initial value of num1.
num2 = num1 / num2; // 1080 / 54 = 20 since num1 = 1080 and num2 = 54
So,
num2 = 20 // which is the initial value of num1
  • Similarly, we divided the num1 by num2 and assigned it to num1. This means that we have filtered out the value of num2 from the multiplied value. So, now num1 contains the initial value of num2.
num1 = num1 / num2; // 1080 / 20 = 54 since num1 = 1080 and num2 = 20
So,
num1 = 54 // which is the initial value of num2

More Examples

C program to find the average/mean of N numbers
Divine Divisors Program using C

Leave a Reply

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