#!
/bin/bash
# 1(b) Create directory and files, list them
mkdir -p mydir
cd mydir || exit
touch file1.txt file2.txt
ls
cd ..
# 2(a) Count lines, words, characters
echo "Enter filename for wc:"
read file
wc "$file"
# 4(b) Count vowels in input
echo "Enter a line of text:"
read line
count=$(echo "$line" | grep -o -i '[aeiou]' | wc -l)
echo "Number of vowels: $count"
# 5(b) Sum and average of four integers
echo "Enter four integers:"
read a b c d
sum=$((a + b + c + d))
avg=$((sum / 4))
echo "Sum: $sum"
echo "Average: $avg"
# 6(b) Simple and compound interest
echo "Enter Principal, Rate and Time:"
read p r t
si=$(( (p * r * t) / 100 ))
ci=$(echo "scale=2; $p * (1 + $r / 100)^$t - $p" | bc)
echo "Simple Interest: $si"
echo "Compound Interest: $ci"
# 7(b) Area and circumference of circle
echo "Enter radius:"
read r
area=$(echo "scale=2; 3.14 * $r * $r" | bc)
circumference=$(echo "scale=2; 2 * 3.14 * $r" | bc)
echo "Area: $area"
echo "Circumference: $circumference"
# 9(a) Student grades
echo "Enter mark:"
read mark
if [ $mark -ge 90 ]; then
echo "Grade: A+"
elif [ $mark -ge 80 ]; then
echo "Grade: A"
elif [ $mark -ge 70 ]; then
echo "Grade: B"
elif [ $mark -ge 60 ]; then
echo "Grade: C"
else
echo "Grade: F"
fi
# 10(b) Fibonacci series
echo "Enter number of terms:"
read n
a=0
b=1
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
# 11(b) Roots of quadratic equation
echo "Enter a, b, c:"
read a b c
d=$((b*b - 4*a*c))
if [ $d -lt 0 ]; then
echo "No real roots"
else
r1=$(echo "scale=2; (-$b + sqrt($d)) / (2*$a)" | bc)
r2=$(echo "scale=2; (-$b - sqrt($d)) / (2*$a)" | bc)
echo "Roots: $r1 and $r2"
fi
# 12(b) Check if number is positive, negative or zero
echo "Enter a number:"
read n
if [ $n -gt 0 ]; then
echo "Positive"
elif [ $n -lt 0 ]; then
echo "Negative"
else
echo "Zero"
fi
# 13(a) Display digits in odd positions
echo "Enter a number:"
read num
len=${#num}
for (( i=0; i<$len; i++ ))
do
if (( $((i % 2)) == 0 )); then
echo -n "${num:$i:1} "
fi
done
echo
# 14(a) Sum using function
sum() {
result=$(( $1 + $2 ))
echo "Sum: $result"
}
echo "Enter two numbers:"
read a b
sum $a $b
# 15(b) Check if number is prime
echo "Enter number:"
read num
is_prime=1
for (( i=2; i*i<=num; i++ ))
do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if (( is_prime == 1 )); then
echo "Prime"
else
echo "Not Prime"
fi
# 19(b) Area of triangle
echo "Enter base and height:"
read base height
area=$(echo "scale=2; 0.5 * $base * $height" | bc)
echo "Area: $area"
# 20(a) Smallest digit in a number
echo "Enter number:"
read num
min=9
for (( i=0; i<${#num}; i++ ))
do
digit=${num:$i:1}
if (( digit < min )); then
min=$digit
fi
done
echo "Smallest digit: $min"
# 23(a) Smallest of two using function
smallest() {
if [ $1 -lt $2 ]; then
echo "$1 is smaller"
else
echo "$2 is smaller"
fi
}
echo "Enter two numbers:"
read a b
smallest $a $b
# 26(b) Fahrenheit to Celsius
echo "Enter temperature in Fahrenheit:"
read f
c=$(echo "scale=2; ($f - 32) * 5 / 9" | bc)
echo "Celsius: $c"