#clanguage #clanguageforbeginners #codeinfarm
Swapping two numbers without using a third variable in C programming is also possible using only mathematical operators. This method is based on the fact that addition and subtraction are reversible operations, and we can use them to perform the swapping.
Here is the code to implement this:
#include stdio.h
int main() {
int a = 5, b = 7;
printf("Before swapping, a = %d and b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping, a = %d and b = %d\n", a, b);
return 0;
}
In this code, we first initialize two variables A and B with their respective values. Then we add A and B and store the result in A. In the next step, we subtract B from A and store the result in B. Finally, we subtract B from A and store the result in A to get the original value of B.
This method works because when we add two numbers and then subtract one of them, we get the value of the other number. For example, if we add 5 and 7, we get 12. If we subtract 5 from 12, we get 7. Similarly, if we subtract 7 from 12, we get 5.
In this video, we have shown how to swap two numbers without using a third variable in C programming using only mathematical operators. This approach can be useful in situations where we have limited memory or where we want to optimize our code.
c program to swap two numbers using third variable, c program to swap two numbers using third variable using functions, program to swap two numbers in c without using third variable
Background music :
Music from #Uppbeat (free for Creators!):
https://uppbeat.io/t/trinity/funk-in-...
License code: MWD5CQTBJBP92MSB
Watch video Swaping Number in C Programming Language | Swap two number in C | Program in C @codeinfarm Hindi online without registration, duration hours minute second in high quality. This video was added by user CodeInFarm 21 February 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 28 once and liked it 2 people.