Program to swap two numbers without using third variable

Swapping means interchanging values of a and b, i.e, value of a in b and b in a.

#include<stdio.h>
void main()
{
int a, b;
printf("Enter value in a and b:");
scanf("%d %d", &a, &b);
a= a+b;
b= a-b;
a= a-b;
printf("\na= %d, b= %d", a,b);
printf("\n Value Swapped");

EXPLANATION:-
a=10 , b=20;
a= a+b=30;
b= a-b= 30-20=10;
a= a-b= 30-10=20;
So, a=20 and b=10 (value swapped)

Post a Comment

0 Comments