what is swapping?
swapping refers to exchanging the values of two variables.
for example if a=1 and b=2.Then swapping is changing value of a to b and b to a.
Here after swapping a=2 and b=1.
By C program too we can swap two values.
I am swapping two variables without using third variable.
Program to swap:
#include<stdio.h>
void main()
{
int a,b;
printf("\n Enter a and b to swap\n");
scanf("%d%d",&a,&b);
\\Logic
a=a+b;
b=a-b;
a=a-b;
printf("\n Swapped values are \ta=%d\tb=%d",a,b);
}
0 comments:
Post a Comment