Overview:
Before, Starting this article First off all, I want to introduce you with Number. Then I will explain What is a Swapping? Furthermore, I will demonstrate and write logic for the swaping of two numbers. 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
- Main Logic for Swapping
- 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.
Main Logic for Swapping
- Step 1: Take two variables as num1 and num2 and store some value to it.
- Step 2: Then, Assign num1 to a temp variable : temp = num1. It becomes num1 free.
- Step 3: Assign num2 to num1 : num1 = num2. It becomes num2 free.
- Step 4: After all, Assign temp to num2 : num2 = temp
Demonstration of Swapping two numbers
C program to swap two numbers
// C program to swap two variables #include int main() { int num1, num2; printf("Enter Value of num1 "); scanf("%d", &x); printf("\nEnter Value of num2 "); scanf("%d", &y); int temp = num1; num1 = num2; num2 = temp; printf("\nAfter the Successful Swapping: num1 = %d, num2 = %d", num1, num2); return 0; }
The output of the Program:
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.