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

Operating System Part3

The document contains code snippets for various mathematical and logical programs written in shell scripting language like Bash. These include programs to calculate factorial of a number, sum of a series, binary to hexadecimal conversion, prime numbers in a range, checking if a number is prime, finding number of words in a string, reversing a number, checking for leap year, calculating sum of natural/square/cube numbers, sorting arrays using different algorithms like bubble, insertion and selection sort, concatenating strings, counting positive/negative/zero numbers in an array, finding LCM and GCD of two numbers, calculating length and substrings of a string, separating alternate digits of a 5 digit number, converting between decimal and octal numbers, checking for

Uploaded by

SAGNIK MIDDYA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Operating System Part3

The document contains code snippets for various mathematical and logical programs written in shell scripting language like Bash. These include programs to calculate factorial of a number, sum of a series, binary to hexadecimal conversion, prime numbers in a range, checking if a number is prime, finding number of words in a string, reversing a number, checking for leap year, calculating sum of natural/square/cube numbers, sorting arrays using different algorithms like bubble, insertion and selection sort, concatenating strings, counting positive/negative/zero numbers in an array, finding LCM and GCD of two numbers, calculating length and substrings of a string, separating alternate digits of a 5 digit number, converting between decimal and octal numbers, checking for

Uploaded by

SAGNIK MIDDYA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

FACTORIAL OF A NUMBER

echo Enter a number

read n

f=1

for((i=1;i<=n;i++))

do

f=$((f*i))

done

echo Factorial is $f
1+1/2+1/3……+1/n SERIES
echo Enter a number

read n

i=1

sum=0

while [ $i -le $n ]

do

sum=`expr $sum + \( 10000 / $i \)`

i=`expr $i + 1`

done

echo Sum n series is

i=1

while [ $i -le 5 ]

do

a=`echo $sum | cut -c $i`

echo -e "$a\c"

if [ $i -eq 1 ] then

echo -e ".\c"

fi

i=`expr $i + 1`

done

echo
BINARY TO HEXADECIMAL

echo Enter a binary number

read n

echo “obase=16; ibase=2; $n”|bc


Prime number in a range 2 to 30
i=2

j=30

flag=0

tem=2

while [ $i -ne $j ]

do

temp=`echo $i`

while [ $temp -ne $tem ]

do

temp=`expr $temp - 1`

n=`expr $i % $temp`

if [ $n -eq 0 -a $flag -eq 0 ]

then

flag=1

fi

done

if [ $flag -eq 0 ]

then

echo $i

else

flag=0
fi

i=`expr $i + 1`

done
PRIME NO
echo Enter a number
read n
i=2
flag=0
while [ $i -lt $n ]
do
m=`expr $n % $i`
if [ $m -eq $flag ]
then
echo Not Prime
exit
else
i=`expr $i + 1`
fi
done
echo Prime
FIND THE NUMBER OF WORDS IN A STRING
echo Enter a text
read text
w='echo $text|wc-w’
echo words=$w

REVERSE NUMBER
echo Enter a number
read n
rev=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
rev=`expr $rev \* 10 + $r`
n=`expr $n / 10`
done
echo The reverse number is $rev
LEAP-YEAR CHECKING
echo enter the year
read y
a=`expr $y % 4`
b=`expr $y % 100`
c=`expr $y % 400`
if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
echo $y is leap year
else
echo $y is not leap year
fi
Sum of n natural no
echo Enter a number
read n
sum=0
for((i=1;i<=n;i++))
do
sum=`expr $sum + $i`
done
echo The sum is $sum
Sum of squre of n natural number
echo Enter a number
read n
sum=0
r=0
for((i=1;i<=n;i++))
do
r=`expr $i \* $i`
sum=`expr $sum + $r`
done
echo The sum of squre is $sum
Sum of cube of n natural number
echo Enter a number
read n
sum=0
r=0
for((i=1;i<=n;i++))
do
r=`expr $i \* $i \* $i`
sum=`expr $sum + $r`
done
echo The sum of cube is $sum
Bubble sort
echo "Enter Values That You Want To Sort:"

read vals

set -- $vals

n=$#

k=0

for val in $*

do

a[$k]=$val

((k++))

done

flag=1

for (( i=0; i<$n-1 && $flag==1; i++ ))

do

flag=0

for (( j=0; j<$n-i-1; j++ ))

do

if [ ${a[$j]} -gt ${a[$j+1]} ]

then

temp=${a[$j]}

a[$j]=${a[$j+1]}

a[$j+1]=$temp
flag=1

fi

done

done

echo "Sorted: "

for (( l=0; l<$n; l++))

do

echo -ne "${a[$l]} "

done

echo
Insertion sort
echo "enter the number"

read n

echo "enter number in an array"

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

do

read arr[$i]

done

for((i=1;i<n;i++))

do

j=$i-1

temp=${arr[$i]}

while((j>=0 && arr[j]>temp))

do

arr[$j+1]=${arr[$j]}

j=$j-1

done

arr[j+1]=$temp

done

echo "printing sorted array"

for((i=0;i<n;i++))
do

echo ${arr[$i]}

done
Selection sort
echo "enter the number"

read n

echo "enter number in an array"

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

do

read arr[$i]

done

for((i=0;i<n-1;i++))

do

small=${arr[$i]}

index=$i

for((j=i+1;j<n;j++))

do

if((arr[j]<small))

then

small=${arr[$j]}

index=$j

fi

done

temp=${arr[$i]}

arr[$i]=${arr[$index]}
arr[$index]=$temp

done

echo "printing sorted array"

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

do

echo ${arr[$i]}

done
Concatenation of two strings
echo Enter 1st string

read x

echo Enter 2nd string

read y

z=$x$y

echo Concatenated strings are $z


Count no of Positive, Negative & Zero
echo Enter length oflist

read n

i=0

echo Enter numbers

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

do

read num[$i]

done

echo The required denominations are:

pos=0

neg=0

zer=0

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

do

if [ ${num[$i]} -gt 0 ]

then

pos=`expr $pos + 1`

elif [ ${num[$i]} -lt 0 ]

then

neg=`expr $neg + 1`

else
zer=`expr $zer + 1`

fi

done

echo Positive=$pos

echo Negative=$neg

echo Zero=$zer
Common Multiple of two numbers
echo "Enter the first number : "

read a

echo "Enter the second number : "

read b

if [ $a -gt $b ]

then

num=$a

den=$b

else

num=$b

den=$a

fi

r=`expr $num % $den`

while [ $r -ne 0 ]

do

num=$den

den=$r

r=`expr $num % $den`

done

gcd=$den

lcm=`expr $a \* $b / $gcd`
echo " The LCM of $a and $b is : $lcm"

Length of a String
echo Enter the string

read str

echo length ${#str}


Generating substring from given length & position of a string
echo Enter a string

read var

echo Enter position of substring

read p

echo Enter length of substring

read n

echo ${var:$p}

echo ${var:$p:$n}
Alternative digits of 5 digit number
echo Enter a 5 digit number

read num

n=1

while [ $n -le 5 ]

do

a=`echo $num | cut -c $n`

echo $a

n=`expr $n + 2`

done
Decimal to Octal conversion
echo Enter a decimal number

read n

echo $n in octal is:

echo “obase=8; ibase=10; $n”|bc


Automorphic number
echo Enter a number

read n

sq=0

r=0

sq=`expr $n \* $n`

echo Squre of the number is $sq

r=`expr $sq % 10`

echo Digit in unit place $r

if [ $n -eq $r ] then

echo Automorphic

else

echo Non-Automorphic

fi
Simple Interest
echo Enter principal

read p

echo Enter time

read n

echo Enter rate of interest

read r

i=`expr $n \* $p \* $r`

i=`expr $i / 100`

echo Simple Interest is $i


Area of square rectangle & circle
echo Enter the side of the square:

read s

echo Enter the length and breadth of rectangle:

read leng

read brea

echo Enter the radius of the circle:

read radius

sarea=`echo $s \* $s | bc`

rarea=`echo $leng \* $brea | bc`

carea=`echo 3.14 \* radius \* radius | bc`

echo Area of the square is: $sarea

echo Area of the rectangle is: $rarea

echo Area of the circle is: $carea

You might also like