Design and test a C program to swap two numbers using a third variable and without using a third variable.

With third variable (temp variable)

#include <stdio.h>
int main()
{
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);

// value of first is assigned to temp
temp = first;

// value of second is assigned to first
first = second;

// value of temp (initial value of first) is assigned to second
second = temp;

printf("\nAfter swapping, first number = %d\n", first);
printf("After swapping, second number = %d\n", second);
return 0;
}
Output


Without third variable

#include <stdio.h>
int main()
{
int first, second;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);

first = first + second;
second = first - second;
first = first - second;

printf("\nAfter swapping, first number = %d\n", first);
printf("After swapping, second number = %d\n", second);
return 0;
}
Output



Try to understand logic