Interview Questions   Tutorials   Discussions   Programs   

C - Swap two integer variables without using a third temporary variable?




458
views
asked marvit May 21, 2014 04:34 AM  

Swap two integer variables without using a third temporary variable?


           

1 Answer



 
answered By experts   0  

#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;
}

flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert