Sunday, September 16, 2012

Code #26:C Program To Swap Two Numbers Without Using Third Variable

| |

#include <stdio.h>
 
int main()
{
   int a, b;
 
   printf("Enter two integers to swap\n");
   scanf("%d%d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
   return 0;
}
To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.

0 comments:

Post a Comment

Powered by Blogger.