Program to Swap Two Numbers | Swapping Program in C language

Spread the love

Differnt-methods-swap-numbers-program

Program Introduction:

Write a Program to swap two numbers in C Language. We will discuss two methods. The First method is swapping two numbers using the temporary variable ( or third variable). The second method is swapping two integers without using any temporary variable.

Example 1:

Input:

Enter two numbers.

Output:

Output after swapping the numbers

Method 1: Swap two Numbers using Temporary Variable :

The program will take two Integers as the input and then swap those two integers. We are going to use the temporary variable to swap the integers.

We will take two integers from user, Lets say. variable 'a' and variable 'b'.

First of all, We are going to store the value of 'a' to a temporary variable named 'temp'.

Then we will copy variable 'b' value to variable 'a'. Now the variable 'b' have the variable 'a' value.

Now use the stored temporary variable 'temp'( temp have the value of 'a' )and copy the 'temp'variable value to the variable 'b’. The variable 'b' got the value of variable 'a'. Finally, variables and 'a'and 'b' values are swapped.

The temporary variable helped us to take variable 'a' value as backup. So that we can copy it to variable 'b' in later step.

But we can also swap the two variables without using any temporary variable. We will discuss about it later in this tutorial.

Program : Swap two numbers using Third variable :

Here is the program to swap two integers with the help of third variable.

Program Output :

From the above output, We can see the give two numbers are swapped.

Let’s look at the another way to swap the numbers without using any extra or third variable.

Method 2 : Swap two numbers without using third or temporary variable:

In this method, We are going to use small math trick to swap the numbers. Here is the algorithm.

Algorithm to swap two numbers without extra variable:

  • Let’s take two variables 'a'and 'b' and We are going to swap these two variables.
  • First of all, We are going to multiply both variables 'a'and variable 'b'. And The result would be 'ab'. We will use this product as the Numerator for our division.
  • Then we are going use a=bOperation. This means we are copying the value of 'b'into the variable 'a'.
  • Now the variable 'a' have the desired value of 'b'.
  • Then in the denominator, We got final value as 'b'.
  • Now we will divide our numerator 'ab’ with the denominator 'b', The result would be value of 'a'
  • Then we are assigning this result value i.e 'a' to variable 'b'. Now the variable 'b'have the desired value of variable 'a'
  • Finally, Two numbers are swapped without using any extra variable.

So by using above algorithm, We can simply swap two integers without using any third variable in a single line of code.

Let’s transform above logic into the code.

Swapping Two Numbers without Extra Variable Program:

Please go through the comments to understand the program.

Program Output:

We are going to compile the program using the gcc compiler. You can learn more about the compiling and running c program in following article. Hello World Program in C language – SillyCodes

Swap-numbers-without-using-third-number

These are the not only ways to swap the two integers, We have many other techniques as well.

Let’s discuss one more bonus method.

Bonus Method : Swapping two numbers using Bitwise XOR Operator:

We can also swap variables by using the bitwise XOR operator without using third variable.

Swap numbers using XOR Operator Algorithm:

Above statement is a complex statement, Which is the combination of following statements.

Okay, Above statements also complex statements. Lets re-write them.

XOR Swapping Algorithm:

  1. Step 1, We apply bitwise XOR on variable 'a' and variable 'b'. And assign the result to variable 'a'.
  2. Step 2: We will apply the bitwise XOR on variable 'b'and variable 'a'. And then assign the result to variable 'b'.
  3. Step 3: We will perform the bitwise XOR on variable 'a' and variable 'b'. And then assign the result to variable 'a'.
  4. After performing above three steps, The values of variable ‘a’ and variable ‘b’ will be swapped.

Program: Swap numbers using Bitwise XOR operator:

Program Output:

We are using GNU GCC compiler to compile and run our programs.

Conclusion:

In this article, We discussed about different ways to swap the numbers. We discussed about the swapping using the temporary variable. We also looked at other methods like using Bitwise XOR to swap the numbers.

Related Programs:

Related Articles:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

5 Responses

  1. May 29, 2021

    […] There are other ways to swap the numbers, We have covered all those methods in the following article. Program to Swap Two Numbers | Swapping Program in C language – SillyCodes […]

  2. May 30, 2021

    […] Program to Swap Two Numbers | Swapping Program in C language – SillyCodes […]

  3. May 31, 2021

    […] Program to Swap Two Numbers | Swapping Program in C language – SillyCodes […]

  4. June 4, 2021

    […] our previous articles, We have discussed about different ways to swap the numbers. In this article, We are going to discuss about the swap two number without using third variable […]

  5. June 5, 2021

    […] Different methods to Swap Two Numbers […]

Leave a Reply