Program to accept two values of variables a & b and Swap their values (WITHOUT using Third variable).| Using C language| very Basic| C Program series 07|
👨💻Program to accept two values of variables a & b and Swap their values. [Without using the Third variable]
Code:
//Program to swap the values of two variables without using third variable.
#include<stdio.h>
main()
{
printf("Program to accept two values of a & b and swap their values.\n");
int a,b;//using only two variables.
printf("Enter the value of a:");
scanf("%d",&a);
printf("Enter the value of b:");
scanf("%d",&b);
printf("\nBEFORE swap a & b: a=%d, b=%d",a,b);
a=a+b;//Change 'a' by addition(sum) of them
b=a-b;//Now here, b=(a+b)-b = a
a=a-b;//Now here, a=(a+b)-(a) = a+b-a = b
printf("\nAFTER swap a & b: a=%d, b=%d",a,b);
}
Output:
Comments
Post a Comment