CocA CocA - 1 month ago 5x
C Question

How to swap two number with using no other variable

I am beginner in programming. I am learning C and our teacher have ask us to do some swapping two number without using any other variables. please help code in C


It is quite simpler than you thought. Here is code:

#include <stdio.h>
int main()
  int x = 10, y = 5;

  // Code to swap 'x' and 'y'
  x = x + y;  // x now becomes 15
  y = x - y;  // y becomes 10
  x = x - y;  // x becomes 5

  printf("After Swapping: x = %d, y = %d", x, y);

  return 0;