Learn about Swapping program in C without using any temporary variable in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Swapping program in C without using any temporary variable
The below C program swaps the given variables without using third variable. The swapping is done thorugh simple addition and subtraction logic.
Program:
#include<stdio.h>
int main()
{
int a=10, b=20;
a = a + b;
b = a - b;
a = a - b;
printf("a=%d \t b=%d\n",a,b);
return 0;
}
Output:
a=20 b=10
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to Swap using a temporary variable
C Program to Find the Square Root of a Number