Variable is a name given to the memory so that whenever we use that variable our computer can understand that. For eg: Let's say a=10 ,it means whenever we use a in our program our computer can recognize as number 10. Variable can store numbers ,characters, etc. depending on which language you are using.
Constants are the values which cannot be changed by the program. Like in the previous example a=10; Value 10 is fixed only we can change that.
Swapping is used in various programs like sorting the array. It is mainly used in the area when we want to store old values without using much space.
So in Conclusion if we have taken a=10 and b=20 then after swapping it should become b=10 and a=20. Swapping can be done using various methods like taking a third variable and then swapping it or without using third variable swapping it through + and - operators or * and / operators. I will be discussing first method using third variable and then without using third variable.
Here in this algorithm we declare 3 variables to store integers ,then we input two numbers lets say 10 and 20. In the computer it gets stored as a=10 and b=20 and we declare a variable c because if we put a=b then the value of b gets stores in a and then value gets removed ,so we put the value of a in c i.e. c=a, so c becomes 10 then we put a=b so a becomes 20. Now we put value of c that stored the value of a in b ,b=a b becomes 10. At last we print the values
Always Remember that the right hand side value of "=" sign gets stored in the left hand side.
Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once.