ASSIGNMENT 5
1. Odd or Even:
echo "Enter an integer:"
read num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even."
else
echo "$num is odd."
fi
2. Leap Year Check:
echo "Enter a year (press Enter to use the current year):"
read year
if [ -z "$year" ]; then
year=$(date +"%Y")
fi
if [ $((year % 4)) -eq 0 ] && { [ $((year % 100)) -ne 0 ] || [ $((year % 400)) -eq 0 ];
}; then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
3. Maximum of Three Numbers:
if [ $# -ne 3 ]; then
echo "Usage: $0 num1 num2 num3"
exit 1
fi
if [ $1 -ge $2 ] && [ $1 -ge $3 ]; then
echo "Maximum is $1"
elif [ $2 -ge $1 ] && [ $2 -ge $3 ]; then
echo "Maximum is $2"
else
echo "Maximum is $3"
fi
4. Prime Number Check:
echo "Enter a number:"
read num
if [ $num -lt 2 ]; then
echo "$num is not a prime number."
exit
fi
for ((i = 2; i * i <= num; i++)); do
if [ $((num % i)) -eq 0 ]; then
echo "$num is not a prime number."
exit
fi
done
echo "$num is a prime number."
Home Assignments
1. Factorial Calculation:
echo "Enter a number:"
read num
fact=1
for ((i = 1; i <= num; i++)); do
fact=$((fact * i))
done
echo "Factorial of $num is $fact."
2. Generate Combinations of 1, 2, and 3:
for i in 1 2 3; do
for j in 1 2 3; do
for k in 1 2 3; do
echo "$i$j$k"
done
done
done
3. Prime Numbers in a Range:
echo "Enter the range (start and end):"
read start end
for ((num = start; num <= end; num++)); do
if [ $num -lt 2 ]; then
continue
fi
is_prime=1
for ((i = 2; i * i <= num; i++)); do
if [ $((num % i)) -eq 0 ]; then
is_prime=0
break
fi
done
if [ $is_prime -eq 1 ]; then
echo $num
fi
done
4. Sum of Digits:
echo "Enter a number:"
read num
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits is $sum."
5. Gross and Take-Home Salary:
echo "Enter BASIC salary:"
read basic
da=$((52 * basic / 100))
hra=$((15 * basic / 100))
pf=$((12 * (basic + da) / 100))
gross_salary=$((basic + da + hra))
take_home=$((gross_salary - pf))
echo "Gross Salary: $gross_salary"
echo "Take Home Salary: $take_home"