C Program to Swap Two Numbers Without Using Third Variable

Swapping is the process of exchanging container values. Suppose container “A” contains Apple and container “B” Banana. Swapping will replace the values of container A with B and vice-versa.

Well above were shitty examples, swapping is used in programming in many ways. Especially in search algorithms. In the search algorithm, many times developer needs to hold the value in temp variables. In such cases, swapping is used. But there we are talking about swapping of numbers that two without using the third variable. The traditional method of swapping involves a third variable which holds the numbers for a while. It is also termed a temp variable.

But today will we will learn how to do swapping of numbers without a third variable.

First, understand What is Swapping and its Uses

What is Swapping?

Swapping means replacing the content of containers. The container may be anything like a box, utensils, bottle, etc.
In C Programming swapping means replacing values of a variable with other variables. Generally, a variable can store an integer value or a float value or even a string. But swapping without the third variable is used mostly in float and numbers.

What are the practical applications of Swapping in Programming?

Swapping is a very small concept and a few lines of code. But many things are almost impossible without the use of swapping. Especially search algorithms and sort algorithms make use of swapping like bubble sort use swapping concept. In modern programming, it’s used in very specific cases where we need to store the value of variable temporary without using persistent space.

Why do we need to learn Swapping logic in C Programming?

Swapping is a very small programming language. You may think it’s not useful. But ask a developer who has worked in core programming for 3-5 Years, he will tell you 100 Places where he would have used swapping logic. In memory-intensive operation, swapping could be used to hold variables temporary and later swap them in the original variables.

Swap Two Numbers Without Using Third Variable in C Programming language

Below is the program of the code swapping program in the C Programming language without using a third variable.

main()
{
int a , b;
printf("Please enter two numbers");
scanf("%d%d",&a,&b);

a = a+b;
b = a-b;
a= a-b;

printf("The value of A is %d and the value of B is %d",a,b);

}

Output:

Please enter two numbers 5 10
The value of A is 10 and the value of B is 5

Programs Exploration

Time needed: 10 minutes

So How to Write Program to Swap Logic without third Variable in C Programming

  1. Declare 2 Variables

    Two variables will be used to take the input from the user and swap the values.

  2. Take the input from the user

    Take the input from the user using scanf statement. Don’t for write the printf statement for a detailed understanding of the user.

  3. A mathematical operation to swap variables without using a third variable

    First, add two numbers, then subtract, please refer code int the blog
    a = a+b;
    b = a-b;
    a= a-b;

  4. Print the result

    Please print the result in the same sequence as that of the input

Writing swapping logic requires the basic knowledge of constants and variables in C Programming. If you don’t know constants and variables in C Programming please go back to the book. If you are in the United States of America or in the United Kingdom you can view the series of C Programming on Amazon Prime. It’s free if you have the Amazon Prime Membership. Please follow these links

  1. C Programming series on Amazon Prime in the United States
  2. C Programming series on Amazon Prime in the United Kingdom

If you are not in the United States or the United Kingdom you can watch the series on C Programming at Newtum. It has some price tag but it’s worth it.

Summary

I hope you have enjoyed C Programming. If you like this article please share it so that, people who are facing problems with writing swapping logic without the third variable can get some help. Happy learning to you.

About The Author