0% found this document useful (0 votes)
51 views10 pages

Simple Shell Programs

The document contains code snippets to calculate the factorial of a given number, find the second largest and second smallest number in an array, sort numbers in an array, and convert a decimal number to hexadecimal. Each code snippet is followed by sample input/output. The code uses Bash scripting to perform the various number calculations and array operations.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
51 views10 pages

Simple Shell Programs

The document contains code snippets to calculate the factorial of a given number, find the second largest and second smallest number in an array, sort numbers in an array, and convert a decimal number to hexadecimal. Each code snippet is followed by sample input/output. The code uses Bash scripting to perform the various number calculations and array operations.
Copyright
© Attribution Non-Commercial (BY-NC)
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 "Factorial for $on is $fact"

OUTPUT: Enter number to find factorial :6 Factorial for 6 is 720

SECOND LARGEST NUMBER FROM AN ARRAY echo "Enter number of elements: " read n echo "Enter array elements:" for((i=0;i<n;i++)) do read a[$i] done for((i=0;i<n;i++)) do for((j=`expr $i + 1`;j<n;j++)) do if [ ${a[$i]} -lt ${a[$j]} ] then temp=${a[$i]} a[$i]=${a[$j]} a[$j]=$temp fi done done echo "The second largest number is ${a[1]}"

OUTPUT: Enter number of elements: 4 Enter array elements: 2 6 8 4 The second largest number is 6

SECOND SMALLEST NUMBER FROM AN ARRAY echo "Enter number of elements: " read n echo "Enter array elements:" for((i=0;i<n;i++)) do read a[$i] done for((i=0;i<n;i++)) do for((j=`expr $i + 1`;j<n;j++)) do if [ ${a[$i]} -gt ${a[$j]} ] then temp=${a[$i]} a[$i]=${a[$j]} a[$j]=$temp fi done done echo "The second smallest number is ${a[1]}"

OUTPUT: Enter number of elements: 5 Enter array elements: 2 5 44 6 1 The second smallest number is 2

SORTING N NUMBERS echo "Enter number of elements: " read n echo "Enter array elements:" for((i=0;i<n;i++)) do read a[$i] done for((i=0;i<n;i++)) do for((j=`expr $i + 1`;j<n;j++)) do if [ ${a[$i]} -gt ${a[$j]} ] then temp=${a[$i]} a[$i]=${a[$j]} a[$j]=$temp fi done done echo "Array after sorting:" for((i=0;i<n;i++)) do echo ${a[$i]} done

OUTPUT: Enter number of elements: 5 Enter array elements: 2 55 33 77 34 Array after sorting: 2 33 34 55 77

DECIMAL TO HEXADECIMAL

n=0 hex=0 echo "Decimal to hexadecimal converter " echo -n "Enter number in decimal format : " read n hex=`echo "obase=16;ibase=10; $n" | bc` echo "$n is equivalent to \"$hex\" in hexadecimal"

OUTPUT: Decimal to hexadecimal converter Enter number in decimal format : 10 10 is equivalent to A in hexadecimal

You might also like