Description:
Two values can be swapped without the use of temporary variable.
ex:
Let a=10, b=5
a=a+b=10+5=15
b=a-b=15-5=10
a=a-b=15-10=5
thus, now a=5 and b=10
Two values can be swapped without the use of temporary variable.
ex:
Let a=10, b=5
a=a+b=10+5=15
b=a-b=15-5=10
a=a-b=15-10=5
thus, now a=5 and b=10
Source Code:
/*
Swapping of two variable without a temporary variable
Author: sourcecodesonline.blogspot.com
*/
#include<stdio.h>
int main(void)
{
int a,b;
printf(“\nEnter the two numbers(a,b) to be swapped”);
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“Now the two numbers have been swapped a=%d b=%d”,a,b);
}
Author: sourcecodesonline.blogspot.com
*/
#include<stdio.h>
int main(void)
{
int a,b;
printf(“\nEnter the two numbers(a,b) to be swapped”);
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“Now the two numbers have been swapped a=%d b=%d”,a,b);
}
EmoticonEmoticon