CocA CocA - 1 year ago 83
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

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download