EVEN OR ODD
read -p "Enter a number: " num;
if [ $((num % 2)) -eq 0 ]; then
echo "even"
else
echo "odd"
fi
OUTPUT
Enter a number: 34
even
BIGGEST OF 2 NUMBERS
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
if (( num1 > num2 )); then
echo "$num1 is bigger."
elif (( num2 > num1 )); then
echo "$num2 is bigger."
else
echo "Both numbers are equal."
fi
OUTPUT
Enter the first number : 12
Enter the second number 10
12 is bigger
BIGGEST OF 3 NUMBERS
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
read -p "Enter the third number: " num3
if (( num1 >= num2 && num1 >= num3 )); then
echo "$num1 is the biggest."
elif (( num2 >= num1 && num2 >= num3 )); then
echo "$num2 is the biggest."
else
echo "$num3 is the biggest."
fi
OUTPUT
Enter the first number : 12
Enter the second number : 10
Enter the third number : 14
14 is the biggest
FACTORIAL
read -p "Enter a number: " num
factorial=1
for (( i=1; i<=num; i++ )); do
factorial=$(( factorial * i ))
done
echo "Factorial of $num is $factorial."
OUTPUT
Enter a number : 5
Factorial of 5 is 120.
FIBONACCI
read -p "Enter the number of terms: " terms
a=0
b=1
if (( terms == 1 )); then
echo " $a"\elif (( terms >= 2 )); then
echo -n " $a $b "
for (( i=3; i<=terms; i++ )); do
c=$(( a + b ))
echo -n "$c "
a=$b
b=$c
done
echo
else
echo "Invalid number of terms."
fi
OUTPUT
Enter number of terms : 8
0 1 1 2 3 5 8 13
ARITHMETIC OPERATIONS
read -p "Enter first number: " num1
read -p "Enter second number: " num2
read -p "Enter operation (+, -, *, /): " op
case $op in
+)
echo "Result: $(( num1 + num2 ))"
;;
-)
echo "Result: $(( num1 - num2 ))"
;;
\*)
echo "Result: $(( num1 * num2 ))"
;;
/)
if (( num2 != 0 )); then
echo "Result: $(( num1 / num2 ))"
else
echo "Division by zero is not allowed."
fi
;;
*)
echo "Invalid operation."
;;
esac
OUTPUT
Enter the first number : 12
Enter the second number : 10
Enter operation : +
Result : 22
MULTIPLICATION TABLE
read -p "Enter a number: " num
for (( i=1; i<=10; i++ )); do
echo "$num x $i = $(( num * i ))"
done
OUTPUT
Enter a number : 3
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
DAYS OF THE WEEK
read -p "Enter a number (1-7): " day
case $day in
1)
echo "Sunday"
;;
2)
echo "Monday"
;;
3)
echo "Tuesday"
;;
4)
echo "Wednesday"
;;
5)
echo "Thursday"
;;
6)
echo "Friday"
;;
7)
echo "Saturday"
;;
*)
echo "Invalid input. Please enter a number
between 1 and 7."
;;
esac
OUTPUT
Enter a number (1-7) : 5
Thursday