1.
Write a Shell program to count the number of vowels in a line of text
# Function to count vowels in a string
count_vowels() {
# Convert the input string to lowercase
lowercase=$(echo "$1" | tr '[:upper:]' '[:lower:]')
# Count the occurrences of vowels
num_vowels=$(echo "$lowercase" | tr -cd 'aeiou' | wc -c)
echo "$num_vowels"
}
# Main script
echo "Enter a line of text:"
read input_text
# Call the function to count vowels
vowel_count=$(count_vowels "$input_text")
echo "Number of vowels in the input text: $vowel_count"
2. Write a shell program to find the sum and average of four integers
calculate_sum() {
sum=$(( $1 + $2 + $3 + $4 ))
echo $sum
}
calculate_average() {
sum=$1
average=$(echo "scale=2; $sum / 4" | bc)
echo $average
}
echo "Enter four integers separated by spaces:"
read num1 num2 num3 num4
sum=$(calculate_sum $num1 $num2 $num3 $num4)
average=$(calculate_average $sum)
echo "Sum of the four integers: $sum"
echo "Average of the four integers: $average"
3. Write a shell program to compute simple interest and compound interest
calculate_simple_interest() {
principle=$1
rate=$2
time=$3
si=$(echo "scale=2; ($principle * $rate * $time) / 100" | bc)
echo $si
}
calculate_compound_interest() {
principle=$1
rate=$2
time=$3
ci=$(echo "scale=2; $principle * (1 + $rate / 100) ^ $time - $principle" | bc)
echo $ci
}
echo "Enter principle amount:"
read principle
echo "Enter rate of interest (in percentage):"
read rate
echo "Enter time period (in years):"
read time
simple_interest=$(calculate_simple_interest $principle $rate $time)
compound_interest=$(calculate_compound_interest $principle $rate $time)
echo "Simple Interest: $simple_interest"
echo "Compound Interest: $compound_interest"
4. Write a Shell program to find the area and circumference of a circle
echo "Enter the radious of the circle"
read r
area=$(echo "3.14*$r*$r" | bc )
circum=$(echo "3.14*2*$r" | bc)
echo "area of the circle is " $area
echo "circumference of the circle is " $circum
5. Write a Shell program to display student grades
echo "*****************"
echo "Student Marksheet"
echo "*****************"
echo "Enter Operating System Marks:"
read os
echo "Enater C++ Marks:"
read cpp
echo "Enater Java Marks:"
read java
echo "*****************"
total=`expr $os + $cpp + $java`
echo "Total Marks:"$total
percentage=`expr $total / 3`
echo "Percentage:" $percentage %
if [ $percentage -ge 60 ]
then
echo "Class: First Class Distinction"
elif [ $percentage -ge 50 ]
then
echo "Class: First class"
elif [ $percentage -ge 40 ]
then
echo "Class: Second class"
else
echo "Class: Fail"
fi
echo "*****************"
6. Write a Shell program to generate Fibonacci series
generate_fibonacci_series() {
n=$1
# Initializing the first two terms of the series
a=0
b=1
echo "Fibonacci series up to $n terms:"
echo -n "$a "
echo -n "$b "
for (( i=2; i<n; i++ )); do
c=$((a + b))
echo -n "$c "
a=$b
b=$c
done
echo
}
echo "Enter the number of terms for Fibonacci series:"
read num_terms
generate_fibonacci_series $num_terms
7. . Write a Shell program to find the roots of a quadratic equation
echo "Enter coefficients of quadratic equation (a, b, c):"
read a b c
delta=$((b*b - 4*a*c))
root1=$(echo "scale=2; (-$b + sqrt($delta)) / (2 * $a)" | bc)
root2=$(echo "scale=2; (-$b - sqrt($delta)) / (2 * $a)" | bc)
echo "Root 1: $root1"
echo "Root 2: $root2"
8. Write a shell script to display the digits which are in odd position in a given number
display_odd_digits() {
number=$1
length=${#number}
echo "Digits at odd positions in $number are:"
for (( i=0; i<$length; i+=2 )); do
echo "${number:i:1}"
done
}
echo "Enter a number:"
read input_number
display_odd_digits $input_number
9. Write a shell program to check whether the given number is positive or
negative.
check_positive_negative() {
if [ $1 -gt 0 ]; then
echo "The number is positive."
elif [ $1 -lt 0 ]; then
echo "The number is negative."
else
echo "The number is zero, neither positive nor negative."
fi
}
echo "Enter a number:"
read number
check_positive_negative $number
[Link] a shell program to find the sum of two numbers using function programming
calculate_sum() {
sum=$(( $1 + $2 ))
echo $sum
}
# Main script
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
result=$(calculate_sum $num1 $num2)
echo "Sum of $num1 and $num2 is: $result"
11. Write a shell program for implementing sequential file allocation strategy
DISK_SIZE=100
current_position=0
allocate_file() {
filename=$1
size=$2
if [ $((current_position + size)) -le $DISK_SIZE ]; then
echo "$filename allocated at position $current_position"
current_position=$((current_position + size))
else
echo "Not enough space to allocate $filename"
fi
}
echo "Sequential File Allocation Strategy"
allocate_file "[Link]" 20
allocate_file "[Link]" 30
allocate_file "[Link]" 15
allocate_file "[Link]" 40
12. Write a Shell script for calculating the area of a triangle
echo -n "Please enter the base and height of the triangle : "
read base
read height
area=`expr "scale=2; 1/2*$base*$height"|bc`
echo "area of the triangle = $area"
13. Write a Shell program to find the smallest digit from a number
echo "Atharv Tiwari"
echo "Enter three numbers:"
read num1
read num2
read num3
smallest=$num1
if [ $num2 -lt $smallest ]; then
smallest=$num2
fi
if [ $num3 -lt $smallest ]; then
smallest=$num3
fi
echo "The smallest number is: $smallest"
14. Write a shell program to find the smallest number between two numbers usingfunction
find_smallest_number() {
if [ $1 -lt $2 ]; then
echo $1
else
echo $2
fi
}
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
smallest=$(find_smallest_number $num1 $num2)
echo "The smallest number between $num1 and $num2 is: $smallest"
15. Write a Shell script for converting the temperature from Fahrenheit to Celsius
fahrenheit_to_celsius() {
fahrenheit=$1
celsius=$(echo "scale=2; ($fahrenheit - 32) * 5 / 9" | bc)
echo $celsius
}
echo "Enter temperature in Fahrenheit:"
read fahrenheit
celsius=$(fahrenheit_to_celsius $fahrenheit)
echo "Temperature in Celsius: $celsius"
16. Develop a Shell program to find whether given number is perfect squareor
Not
clear
echo -n "Enter the Number==> "
read n
v=0
i=1
x=0
while test $i -le $n
do
x=`expr $i \* $i`
if test "$x" = "$n"
then
v=1
echo "The Number is a perfect square"
fi
i=`expr $i + 1`
done
if test $v -eq 0
then
echo "The Number is not a perfect square"
fi
17. Write a shell program to display first 10 natural numbers
echo "Using while loop..."
j=1
while [ $j -le 10 ]
do
echo -n "$j "
j=$((j + 1)) # increase number by 1
done
echo ""