Home Uncategorized How to swap two numbers without using a temporary variable

How to swap two numbers without using a temporary variable

SHARE

Write a C Program to Swap two numbers without third variable.

#include<stdio.h>
int main()
{
  int a,b;
  printf(“Enter two numbers :\n”);
  scanf(“%d %d”,&a,&b);
  printf(“Before Swapping : a = %d, b = %d\n”,a,b);
  a=a+b;
  b=a-b;
  a=a-b;
  printf(“Before Swapping : a = %d, b = %d\n”,a,b);
  return 0;
}

Output :
i) Enter two numbers :
65    9521
Before Swapping : a = 65, b = 9521
After Swapping : a = 9521, b = 65

Back to the programs.