Google Search

Wednesday, October 27, 2010

INTERCHANGING TWO NUMBERS

To write a shell program to interchange two numbers using a temporary variable and without using a temporary variable.

Algorithm
Using the Temporary Variable
1.Enter the two numbers.
2.Declare the temporary variable ‘C’ and do tne tollowing assignments
c=a, a=bandb=c.
3.Display the result.
Without using temporary variable
1.Enter the two numbers by using a and b variable.
2. Then using the expressions.
a= a+b,b=a—banda=a-b, values areinterchanged.
3.Display the result.

IProgram: 1
#Swap using Temporary variable echo “swapping using temporary variable”
echo “Enter a”
read a
echo “Enter b”
read b
c = $a
a = $b
b = $c
echo “After swapping”
echo “Number a is $a”
echo “Number b is $b”

program 2 (using the Tempoary Variable)
Using Temporary variable
Program : 2
#swap without Temporary variable
echo Swapping without using temporary variable echo Enter a
read a
echo Enter b
read b
a = expr $a + $b’
b = ‘expr $a - $b’
a = ‘expr $a - $b’
echo After swapping
echo a is $a
echo b is $b



Output
Swapping using temporary variable
Enter a
10
Enter b
15
After swapping ais 15
bis 10
Swapping without using temporary variable
Enter a
6
Enter b
7
After swapping ais7
b is 6

0 comments:

Post a Comment