Swapping is the process of exchanging the container values. Suppose container “A” contains Apple and container “B” Banana. Swapping will replace the values of container A into B and vice-versa.
Well above was 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 as temp variable.
But today will we will learn how to do swapping of numbers without third variable.
First understand What is Swapping and it’s Uses
Swapping mean replacing the content of containers. 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.
Swapping is very small concepts and a few lines of codes. 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.
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 use swapping logic.
In memory-intensive operation swapping could be used to hold variable temporary and later swap them in the original variables.
Writing Swapping logic without third variable in C Programming language
Writing swapping logic requires the basic knowledge of constants and variables in C Programming. If you don’t know constant and variables in C Programming please go back to book.
If you are in 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
- C Programming series on Amazon Prime in United States.
- C Programming series on Amazon Prime in United Kingdom
If you are not in United States or United Kingdom you can watch the series on C Programming at Newtum. Its has some price tag but it’s worth.
Below is the source the code swapping program in C Programming language. For detail explanation please refer the How to Section at the Bottom.
int a , b;
printf(“Please enter two numbers”);
a = a+b;
b = a-b;
printf(“Value of A is %d and Value of B is %d”,a,b);
Please enter two numbers 5 10
Value of A is 10 and Value of B is 5
How to Section explains each program of the C Programming in Details
Time needed: 10 minutes.
S How to Write Swapping Logic without third Variable in C Programming Language
- Declare 2 Variables
Two variables will be used to take the input from the user and swap the values
- Take the input from user
Take the input form user using scanf statement . Don’t for to write the printf statement for detail understanding of the user.
- Mathematical operation to swap variables without using third variable
First add two numbers, then subtract, please refer code int the blog
a = a+b;
b = a-b;
- Print the result
Please print the result in the same sequence as that of input
I hope you have enjoyed C Programming. If you like this article please share so that, people who are facing problems with writing swapping logic without the third variable can get some help.
Happy learning to you.