0% found this document useful (0 votes)
26 views

Shell Programs

The document contains code snippets for calculating the factorial of a number, finding the second largest number in an array, sorting numbers in an array, and converting a decimal number to hexadecimal. It provides the code to implement each operation and sample outputs showing the code running on test input values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Shell Programs

The document contains code snippets for calculating the factorial of a number, finding the second largest number in an array, sorting numbers in an array, and converting a decimal number to hexadecimal. It provides the code to implement each operation and sample outputs showing the code running on test input values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Factorial of given number

n=0
on=0
fact=1
echo "Enter number to find factorial :"
read n
on=$n
while [ $n -ge 1 ]
do
fact=`expr $fact $n`
n=`expr $n - 1`
done
echo "!actorial for $on i" $fact"
OUTPUT:
Enter number to find factorial :#
!actorial for # i" $%0
Second largest number from an array
echo "Enter number of element": "
read n
echo "Enter arra& element":"
for''i=0(i)n(i**++
do
read a[$i]
done
for''i=0(i)n(i**++
do
for'',=`expr $i * 1`(,)n(,**++
do
if [ $-a[$i]. -lt $-a[$,]. ]
then
temp=$-a[$i].
a[$i]=$-a[$,].
a[$,]=$temp
fi
done
done
echo "/he "econd large"t number i" $-a[1]."
OUTPUT:
Enter number of element":
0
Enter arra& element":
%
#
1
0
/he "econd large"t number i" #
Second largest number from an array
echo "Enter number of element": "
read n
echo "Enter arra& element":"
for''i=0(i)n(i**++
do
read a[$i]
done
for''i=0(i)n(i**++
do
for'',=`expr $i * 1`(,)n(,**++
do
if [ $-a[$i]. -gt $-a[$,]. ]
then
temp=$-a[$i].
a[$i]=$-a[$,].
a[$,]=$temp
fi
done
done
echo "/he "econd "malle"t number i" $-a[1]."
OUTPUT:
Enter number of element":
2
Enter arra& element":
%
2
00
#
1
/he "econd "malle"t number i" %
Sorting N numbers
echo "Enter number of element": "
read n
echo "Enter arra& element":"
for''i=0(i)n(i**++
do
read a[$i]
done
for''i=0(i)n(i**++
do
for'',=`expr $i * 1`(,)n(,**++
do
if [ $-a[$i]. -gt $-a[$,]. ]
then
temp=$-a[$i].
a[$i]=$-a[$,].
a[$,]=$temp
fi
done
done
echo "3rra& after "orting:"
for''i=0(i)n(i**++
do
echo $-a[$i].
done
OUTPUT:
Enter number of element":
2
Enter arra& element":
%
22
44
$$
40
3rra& after "orting:
%
44
40
22
$$
Base Convertion

n=0
hex=0
echo "5ecimal to hexadecimal con6erter "
echo -n "Enter number in decimal format : "
read n
hex=`echo "oba"e=1#(iba"e=10( $n" 7 bc`
echo "$n i" e8ui6alent to 9"$hex9" in hexadecimal"
OUTPUT:
5ecimal to hexadecimal con6erter
Enter number in decimal format :
10
10 i" e8ui6alent to :3 : in hexadecimal

You might also like