C Program to swap two numbers without using third variable

In this example, we will see C Program to swap two numbers without using third variable.

C Program to swap two numbers without using third variable


/* C-Program: Read two integer values & swap these values take without third variable */
#include <stdio.h>

int main()
{
int a,b;
printf("Enter the values of a and b:\n");
scanf("%d%d", &a,&b);
printf("\nbefore swapping:\n");
printf("a = %d\tb = %d\n", a,b);
/* swap logic */
a = a+b;
b = a-b;
a = a-b;
printf("\nafter swapping:\n");
printf("a = %d\tb = %d\n", a,b);
return 0;
}

Output :-

Enter the values of a and b:
5 7

before swapping:
a = 5  b = 7

after swapping:
a = 7  b = 5
Related Articles

Leave a Reply