Overview:
In this article First of all, I want to introduce you with Numbers. Then I will explain What is Swapping? Furthermore, I will demonstrate and write logic for the swaping of two numbers without third variable. After all, I will show a C Program to swap two numbers.
Table of contents:
- What is a Number?
- What is swapping?
- Demonstration of Swapping two numbers
- How to swap values of two variables without third variable?
- C program to swap two integers
- Conclusion
What is a Number?
Number is an object that uses digits to perform mathematical task. Calculus that is a branch of mathematics included many numbers such as integer, whole number, real number, and imaginary numbers so on. Here, I will discuss about number to perform arithmetic addition operation. Moreover, Number is a combination of digits, some symbols and decimal point. For instance, 127.23 is a rational number.
What is swapping?
Swapping is a mechanism by which you can exchange the value of two variables. For instance, If First number has value 13 and Second number has value 26 Then, swapping stores 26 to First number and 13 to Second number.
How to swap values of two variables without third variable?
- Step 1: Take two variables as num1 and num2 and store some value to it.
- Step 2: Then, Assign num1+num2 to a num1.
- Step 3: Assign num1-num2 to num2.
- Step 4: Assign num1-num2 to num1. After all, display num1 and num2.
Demonstration of Swapping two numbers
C program to swap two numbers without third variable
#include int main () { int a=10, b=20; printf ("Before swap a=%d b=%d",a,b); a=a+b; b=a-b; a=a-b; printf ("\nAfter swap a=%d b=%d",a,b); return 0; }
The output of the Program:
Before swap a=10 b=20
After swap a=20 b=10
Conclusion:
This Program stores two integers one by one into the int datatype. After the successful swapping of numbers it will exchange the value of them at once or you can say that it assign a's value to b and b's value to a.