Swapping Two numbers in C without using a third variable

Video 8 of 82023-12-12
Course Progress100%

Code & Notes

Swapping Two numbers in C without using a third variable

#include <stdio.h>

int main() {
	int x = 2;
	int y = 5;

	// main logic
	x = x + y; // store the sum of two numbers into x
	y = x - y; // store the subtraction of y from new x value into y
	x = x - y; // finally do the same with updated y value

    // printing the output
	printf("x = %d\n", x);
	printf("y = %d", y);
	return 0;
}