By using three variables it will perform the swap operation. These basic swap operation. And you can do also perform swapping with two variables. a=a+b, a>b,b=a-b, a=a-b;
Example a=7,b=5, a=7+5; a=12 ,b=12-5=7,a=12 -7=5 finally a=5 and b=7 these swap operation perform by using only two variable.
Source code bash shell Programming Algorithm
echo “ First value ”
read a
echo “ Second value ”
read b
temp=$a
a=$b
b=$temp
echo “ The swapped value: ”
echo “ First value = $a ”
echo “ Second value = $b ”
using two variable to perform the swap operation
a=5,b=7
$a=$a+$b;(5+7=12)
$b=$a-$b;(12-7=5)
$a=$a-$b;(12-5=7)
OUPUT CS1252 Operating Systems lap
[redhat35@localhost Rhel5]$ sh swapping.sh
First value
23
Second value
10
The swapped value is:
First value = 10
Second value = 23
Example a=7,b=5, a=7+5; a=12 ,b=12-5=7,a=12 -7=5 finally a=5 and b=7 these swap operation perform by using only two variable.
Source code bash shell Programming Algorithm
echo “ First value ”
read a
echo “ Second value ”
read b
temp=$a
a=$b
b=$temp
echo “ The swapped value: ”
echo “ First value = $a ”
echo “ Second value = $b ”
using two variable to perform the swap operation
a=5,b=7
$a=$a+$b;(5+7=12)
$b=$a-$b;(12-7=5)
$a=$a-$b;(12-5=7)
OUPUT CS1252 Operating Systems lap
[redhat35@localhost Rhel5]$ sh swapping.sh
First value
23
Second value
10
The swapped value is:
First value = 10
Second value = 23
0 Comments