7) C Program to swap two number:-
(A) BY USING THIRD VARIABLE:-
LOGIC:- let there are two variable a=10,b=20, by swapping we want variable a =20 and b=10.Here we use third variable c. In c we assign any one variable from a and b (c=a or c=b),then other variable which we did not assign in c .Assign that variable in that variable which is assign into c.
EXAMPLE:-
(B) WITHOUT USING THIRD VARIABLE:-
LOGIC:- now swapping is done by using only a and b.In this case 3 steps are required.
1) a=a+b;(a=10+20,a=30).
2) b=a-b(b=30-20=>10 which is equal to = a initial value) and
3) last if we apply a=a-b(a=30-10=>20 which is equal to = b initial value)
EXAMPLE:-
(A) BY USING THIRD VARIABLE:-
LOGIC:- let there are two variable a=10,b=20, by swapping we want variable a =20 and b=10.Here we use third variable c. In c we assign any one variable from a and b (c=a or c=b),then other variable which we did not assign in c .Assign that variable in that variable which is assign into c.
EXAMPLE:-
(B) WITHOUT USING THIRD VARIABLE:-
LOGIC:- now swapping is done by using only a and b.In this case 3 steps are required.
1) a=a+b;(a=10+20,a=30).
2) b=a-b(b=30-20=>10 which is equal to = a initial value) and
3) last if we apply a=a-b(a=30-10=>20 which is equal to = b initial value)
EXAMPLE:-
TASK:-"""TRY SWAPPING A CHARACTER"""
Comments
Post a Comment