Os Experiment 11

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

72.

#!/bin/bash
n=$1
# make sure command line arguments are passed to the script
if [ $# -eq 0 ]
then
echo "A shell script to print multiplication table."
echo "Usage : $0 number"
exit 1
fi

# Use for loop


for i in {1..10}
do
echo "$n * $i = $(( $i * $n))"
done

Save and close the file. Run it as follows:

chmod +x multiplication.sh
./multiplication.sh
./multiplication.sh 13

Sample outputs:

13 * 1 = 13
13 * 2 = 26
13 * 3 = 39
13 * 4 = 52
13 * 5 = 65
13 * 6 = 78
13 * 7 = 91
13 * 8 = 104
13 * 9 = 117
13 * 10 = 130

72.2

1
#!/bin/bash
n=6
a=$n/2
for((i=$a-1;i>=0;i–))
do
for((k=i; k<=$n; k++))
do
echo -ne ” “;
done
for((j=1;j<=2*i-1;j++))
do
echo -ne “*”;
done
echo;
done

73.2

BASIS FOR
RECURSION ITERATION
COMPARISON

Basic The statement in a body of function calls the Allows the set of instructions to be repeatedly
function itself. executed.

Format In recursive function, only termination Iteration includes initialization, condition,


condition (base case) is specified. execution of statement within loop and update
(increments and decrements) the control
variable.

Termination A conditional statement is included in the body The iteration statement is repeatedly executed
of the function to force the function to return until a certain condition is reached.
without recursion call being executed.

Condition If the function does not converge to some If the control condition in the iteration statement
condition called (base case), it leads to infinite never become false, it leads to infinite iteration.
recursion.

Infinite Infinite recursion can crash the system. Infinite loop uses CPU cycles repeatedly.
Repetition

Applied Recursion is always applied to functions. Iteration is applied to iteration statements or


"loops".

Stack The stack is used to store the set of new local Does not uses stack.
variables and parameters each time the
function is called.

Overhead Recursion possesses the overhead of repeated No overhead of repeated function call.

2
BASIS FOR
RECURSION ITERATION
COMPARISON

function calls.

Speed Slow in execution. Fast in execution.

Size of Code Recursion reduces the size of the code. Iteration makes the code longer.

73.3
N=6

a=0

b=1

echo "The Fibonacci series is : "

for (( i=0; i<N; i++ ))


do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done

Input : 5
Output :
Fibonacci Series is :
0
1
1
2
3

73.4

echo "Enter the number:"


read i
echo "$i table"
j=1
while [ $i -le 10 ]
do
l=`expr $j \* $j = $l`
echo "$j *$i = $l"
j=`expr $j + 1`
3
done

OUTPUT:

Enter the number:


2
2 table
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 =20

73.5

Num=123
g=$Num

# store the sum of


# digits
s=0

# use while loop to


# caclulate the sum
# of all digits
while [ $Num -gt 0 ]
do
# get Remainder
k=$(( $Num % 10 ))

# get next digit


Num=$(( $Num / 10 ))

# calculate sum of
# digit
s=$(( $s + $k ))
done
echo "sum of digits of $g is : $s"

Output
sum of digits of 123 is : 6

74.1
4
for day in Sunday Saturday Monday Tuesday Wednesday Thursday Friday
do
echo "Welcome $day day"
done

output
Welcome Sunday day
Welcome Saturday day
Welcome Monday day
Welcome Tuesday day
Welcome Wednesday day
Welcome Thursday day
Welcome Friday day

You might also like