Exercise 1: Write a shell script that calculates the factorial of a user-provided number using a for loop.
#!/bin/bash
echo "Enter a number:"
read num
factorial=1
for (( i=1; i<=num; i++ ))
do
factorial=$(( factorial * i ))
done
echo "Factorial of $num is $factorial"
Exercise 2: Develop a seript that prints the multiplication table for a given number up to 10 using a for loop.
#!/bin/bash
echo "Enter a number to print its multiplication table:"
read num
for (( i=1; i<=10; i++ ))
do
echo "$num x $i = $(( num * i ))"
done
Exercise 3: Create a wript that reverses a given sumber using a while loop.
#!/bin/bash
echo "Enter a number to reverse:"
read num
reverse=0
while [ $num -gt 0 ]
do
remainder=$(( num % 10 ))
reverse=$(( reverse * 10 + remainder ))
num=$(( num / 10 ))
done
echo "Reversed number is $reverse"
Exercise 4: Wire a script to display all files in a directory usmg a for loop.
#!/bin/bash
echo "Enter directory path:"
read dir
if [ -d "$dir" ]; then
for file in "$dir"/*
do
echo "$file"
done
else
echo "Directory does not exist."
fi
#!/bin/bash
echo "Select an arithmetic operation:"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read choice
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
case $choice in
1) result=$((num1 + num2))
echo "Result: $num1 + $num2 = $result" ;;
2) result=$((num1 - num2))
echo "Result: $num1 - $num2 = $result" ;;
3) result=$((num1 * num2))
echo "Result: $num1 * $num2 = $result" ;;
4) if [ $num2 -ne 0 ]; then
result=$((num1 / num2))
echo "Result: $num1 / $num2 = $result"
else
echo "Division by zero is not allowed."
fi ;;
*) echo "Invalid choice." ;;
esac
