Hello Friend’s
Today we are going to learn
about how to swap two number without using third variable this is very easy am
going to tell you how?
Simply we are take two
variable suppose “a” and “b” then we are going to use formula or relation
between ‘a’ and ‘b’ . those formula are like that
Simply we are make a=a+b therefore
‘a’ has both the value and then we are substracting ‘b’ by a then we get ‘b’ and
same thing done by ‘a’ and we are able to swap the number . below am going to
write in mathematical form
- a=a+b
- b=a-b
- a=a-b
I hope so now you can understand well
So now we are write code for that …..
#include<stdio.h>
int main(){
int a,b;
printf("enter the value of a=");
scanf("%d",&a);
printf("enter the value of b=");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swap\n");
printf("a=%d,b=%d",a,b);
return 0;
}
OUTPUT:-
enter the value of
a=12
enter the value of
b=3
value of a and b
after swap
a=3,b=12

0 Comments