C Program Swap Two Numbers Without Third Variable

By | March 6, 2020

C Program Swap Two Numbers Without

#include <stdio.h>
int main()
{
  
  int n1,n2;

  printf("Enter two numbers to swap values without using third variable\n");
  scanf("%d%d", &n1, &n2);

  printf("Before swapping: n1=%d and n2=%d", n1,n2);
  
  n1 = n1 + n2;
  n2 = n1 - n2;
  n1 = n1 - n2;
  
  
  printf("\nAfter  swapping: n1=%d and n2=%d", n1,n2);
  

  return 0;
}

Output

Enter two numbers to swap values without using third variable
100
200
Before swapping: n1=100 and n2=200
After swapping: n1=200 and n2=100
Press Enter to return to Quincy…

Leave a Reply

Your email address will not be published. Required fields are marked *