Trading of two numbers in C Language is the interaction where the worth of two factors is traded utilizing some code. For instance,
We can trade two numbers in different ways as follows:
- Swapping two variable values using a Temporary Variable
- Swapping two variable values using Addition and Subtraction
- Swapping two variable values using Bitwise Operator
- Swapping two variable values using Multiplication and Division
1. Swapping two numbers in C using a temporary Variable
We should begin with the calculation steps first,
Algorithm:
- declare three variables x, y and temp
- take input in x and y, say x = 5 and y = 7
- assign the value of x to temp, say 5
- now temp = 5 and x = 5
- put the value of y in x, so y = 7 and x = 7
- then, put the value of temp in y, so temp = 5 and y = 5
The following is a program to trade two numbers utilizing brief variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 10, y = 15, temp;
temp = x;
x = y;
y = temp;
printf(“x = %d and y = %d”, x, y);
getch();
}
2. Swapping two numbers using Addition and Subtraction
We should begin with the calculation steps first,
Algorithm:
- Take input of the two numbers, say x = 5 and y = 7
- Store the sum of both the numbers in the first number(x = 5 + 7 so x = 12) and store the difference of both the numbers in the second number(y = 12 – 7, so y = 5).
- Then store their difference in the first number(x = 12 – 5 so x = 7) and print.
The following is a program to trade two numbers without utilizing any transitory variable, and use expansion and deduction administrator for making it happen.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 10, y = 15;
x = x + y – (y = x);
printf(“x = %d and y = %d”,x,y);
getch();
}
3. Swapping two numbers using Bitwise Operator
XOR gives yield as 1 when two unique pieces are XORed and give 0 when two same pieces are XORed. The XOR of two numbers x and y returns a number that has every one of the pieces as 1 any place pieces of x and y vary. For instance, XOR of 7 (0111) and 5 (0101) is (0010).
Algorithm:
- Input two numbers, say x = 5 and y = 7
- XOR the two numbers and store the result in the first number(x = 5 ^ 7 so x = 2)
- XOR the two numbers again and store the result in the second number (y = 2 ^ 7 so y = 5)
- XOR the two numbers again and store the result in the first number (x = 2 ^ 5 so x = 7)
The following is the program to trade two numbers utilizing bitwise administrator.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 6, y = 4;
x = x^y;
y = x^y;
x = x^y;
printf(“x = %d and y = %d”, x, y);
getch();
}
4. Swapping two numbers using Multiplication and Division
How about we start with the calculation steps first,
Algorithm:
- Take input of the two numbers, say x = 5 and y = 7
- Store the product of both the numbers in the first number(x = 5 * 7 so x = 35) and store the quotient of both the numbers in the second number(y = 35 / 7, so y = 5).
- Then store their difference in the first number(x = 35 / 5 so x = 7) and print.
The following is the program to trade two numbers utilizing duplication and division.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 6, y = 4;
x = x*y;
y = x/y;
x = x/y;
printf(“x = %d and y = %d”, x, y);
getch();
}
Points to Remember
- In the algorithm using addition and division and XOR, if the values are very big, it can result in integer overflow.
- In the algorithm using division and multiplication, if one of the values is zero, the product will become zero and the algorithm will fail.