C program for swapping of two Variables without using temporary variable

Logic:
         To swap two variable we will add the both variables and store in first variable, then we will subtract second from first and store in second so we will get value of first variable in Second Variable. Then from value of first variable which is still the sum of 2 value we will subtract the value of second variable. so we will get the value of second variable in first variable.

Code:

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

Post a Comment

CodeNirvana
Newer Posts Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top