Thursday 20 September 2012

Swapping of two numbers without third variable

You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-



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

No comments:

Post a Comment